-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.json
45 lines (45 loc) · 11.7 KB
/
package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{
"name": "faye-websocket",
"description": "Standards-compliant WebSocket server and client",
"homepage": "http://github.com/faye/faye-websocket-node",
"author": {
"name": "James Coglan",
"email": "jcoglan@gmail.com",
"url": "http://jcoglan.com/"
},
"keywords": [
"websocket",
"eventsource"
],
"license": "MIT",
"version": "0.7.0",
"engines": {
"node": ">=0.4.0"
},
"main": "./lib/faye/websocket",
"dependencies": {
"websocket-driver": ">=0.3.0"
},
"devDependencies": {
"jstest": "",
"pace": ""
},
"scripts": {
"test": "jstest spec/runner.js"
},
"repository": {
"type": "git",
"url": "git://github.com/faye/faye-websocket-node.git"
},
"bugs": {
"url": "http://github.com/faye/faye-websocket-node/issues"
},
"readme": "# faye-websocket\n\n* Travis CI build: [![Build\n status](https://secure.travis-ci.org/faye/faye-websocket-node.png)](http://travis-ci.org/faye/faye-websocket-node)\n* Autobahn tests: [server](http://faye.jcoglan.com/autobahn/servers/),\n [client](http://faye.jcoglan.com/autobahn/clients/)\n\nThis is a general-purpose WebSocket implementation extracted from the\n[Faye](http://faye.jcoglan.com) project. It provides classes for easily\nbuilding WebSocket servers and clients in Node. It does not provide a server\nitself, but rather makes it easy to handle WebSocket connections within an\nexisting [Node](http://nodejs.org/) application. It does not provide any\nabstraction other than the standard [WebSocket\nAPI](http://dev.w3.org/html5/websockets/).\n\nIt also provides an abstraction for handling\n[EventSource](http://dev.w3.org/html5/eventsource/) connections, which are\none-way connections that allow the server to push data to the client. They are\nbased on streaming HTTP responses and can be easier to access via proxies than\nWebSockets.\n\n\n## Installation\n\n```\n$ npm install faye-websocket\n```\n\n\n## Handling WebSocket connections in Node\n\nYou can handle WebSockets on the server side by listening for HTTP Upgrade\nrequests, and creating a new socket for the request. This socket object exposes\nthe usual WebSocket methods for receiving and sending messages. For example this\nis how you'd implement an echo server:\n\n```js\nvar WebSocket = require('faye-websocket'),\n http = require('http');\n\nvar server = http.createServer();\n\nserver.on('upgrade', function(request, socket, body) {\n if (WebSocket.isWebSocket(request)) {\n var ws = new WebSocket(request, socket, body);\n \n ws.on('message', function(event) {\n ws.send(event.data);\n });\n \n ws.on('close', function(event) {\n console.log('close', event.code, event.reason);\n ws = null;\n });\n }\n});\n\nserver.listen(8000);\n```\n\n`WebSocket` objects are also duplex streams, so you could replace the\n`ws.on('message', ...)` line with:\n\n```js\n ws.pipe(ws);\n```\n\nNote that under certain circumstances (notably a draft-76 client connecting\nthrough an HTTP proxy), the WebSocket handshake will not be complete after you\ncall `new WebSocket()` because the server will not have received the entire\nhandshake from the client yet. In this case, calls to `ws.send()` will buffer\nthe message in memory until the handshake is complete, at which point any\nbuffered messages will be sent to the client.\n\nIf you need to detect when the WebSocket handshake is complete, you can use the\n`onopen` event.\n\nIf the connection's protocol version supports it, you can call `ws.ping()` to\nsend a ping message and wait for the client's response. This method takes a\nmessage string, and an optional callback that fires when a matching pong\nmessage is received. It returns `true` iff a ping message was sent. If the\nclient does not support ping/pong, this method sends no data and returns\n`false`.\n\n```js\nws.ping('Mic check, one, two', function() {\n // fires when pong is received\n});\n```\n\n\n## Using the WebSocket client\n\nThe client supports both the plain-text `ws` protocol and the encrypted `wss`\nprotocol, and has exactly the same interface as a socket you would use in a web\nbrowser. On the wire it identifies itself as `hybi-13`.\n\n```js\nvar WebSocket = require('faye-websocket'),\n ws = new WebSocket.Client('ws://www.example.com/');\n\nws.on('open', function(event) {\n console.log('open');\n ws.send('Hello, world!');\n});\n\nws.on('message', function(event) {\n console.log('message', event.data);\n});\n\nws.on('close', function(event) {\n console.log('close', event.code, event.reason);\n ws = null;\n});\n```\n\nThe WebSocket client also lets you inspect the status and headers of the\nhandshake response via its `statusCode` and `headers` properties.\n\n\n## Subprotocol negotiation\n\nThe WebSocket protocol allows peers to select and identify the application\nprotocol to use over the connection. On the client side, you can set which\nprotocols the client accepts by passing a list of protocol names when you\nconstruct the socket:\n\n```js\nvar ws = new WebSocket.Client('ws://www.example.com/', ['irc', 'amqp']);\n```\n\nOn the server side, you can likewise pass in the list of protocols the server\nsupports after the other constructor arguments:\n\n```js\nvar ws = new WebSocket(request, socket, body, ['irc', 'amqp']);\n```\n\nIf the client and server agree on a protocol, both the client- and server-side\nsocket objects expose the selected protocol through the `ws.protocol` property.\n\n\n## Initialization options\n\nBoth the server- and client-side classes allow an options object to be passed\nin at initialization time, for example:\n\n```js\nvar ws = new WebSocket(request, socket, body, protocols, options);\nvar ws = new WebSocket.Client(url, protocols, options);\n```\n\n`protocols` is an array of subprotocols as described above, or `null`.\n`options` is an optional object containing any of these fields:\n\n* `headers` - an object containing key-value pairs representing HTTP headers to\n be sent during the handshake process\n* `ping` - an integer that sets how often the WebSocket should send ping\n frames, measured in seconds\n\n\n## WebSocket API\n\nBoth server- and client-side `WebSocket` objects support the following API.\n\n* <b>`on('open', function(event) {})`</b> fires when the socket connection is\n established. Event has no attributes.\n* <b>`on('message', function(event) {})`</b> fires when the socket receives a\n message. Event has one attribute, <b>`data`</b>, which is either a `String`\n (for text frames) or a `Buffer` (for binary frames).\n* <b>`on('error', function(event) {})`</b> fires when there is a protocol error\n due to bad data sent by the other peer. This event is purely informational,\n you do not need to implement error recover.\n* <b>`on('close', function(event) {})`</b> fires when either the client or the\n server closes the connection. Event has two optional attributes,\n <b>`code`</b> and <b>`reason`</b>, that expose the status code and message\n sent by the peer that closed the connection.\n* <b>`send(message)`</b> accepts either a `String` or a `Buffer` and sends a\n text or binary message over the connection to the other peer.\n* <b>`ping(message = '', function() {})`</b> sends a ping frame with an\n optional message and fires the callback when a matching pong is received.\n* <b>`close(code, reason)`</b> closes the connection, sending the given status\n code and reason text, both of which are optional.\n* <b>`version`</b> is a string containing the version of the `WebSocket`\n protocol the connection is using.\n* <b>`protocol`</b> is a string (which may be empty) identifying the\n subprotocol the socket is using.\n\n\n## Handling EventSource connections in Node\n\nEventSource connections provide a very similar interface, although because they\nonly allow the server to send data to the client, there is no `onmessage` API.\nEventSource allows the server to push text messages to the client, where each\nmessage has an optional event-type and ID.\n\n```js\nvar WebSocket = require('faye-websocket'),\n EventSource = WebSocket.EventSource,\n http = require('http');\n\nvar server = http.createServer();\n\nserver.on('request', function(request, response) {\n if (EventSource.isEventSource(request)) {\n var es = new EventSource(request, response);\n console.log('open', es.url, es.lastEventId);\n \n // Periodically send messages\n var loop = setInterval(function() { es.send('Hello') }, 1000);\n \n es.on('close', function() {\n clearInterval(loop);\n es = null;\n });\n \n } else {\n // Normal HTTP request\n response.writeHead(200, {'Content-Type': 'text/plain'});\n response.end('Hello');\n }\n});\n\nserver.listen(8000);\n```\n\nThe `send` method takes two optional parameters, `event` and `id`. The default\nevent-type is `'message'` with no ID. For example, to send a `notification`\nevent with ID `99`:\n\n```js\nes.send('Breaking News!', {event: 'notification', id: '99'});\n```\n\nThe `EventSource` object exposes the following properties:\n\n* <b>`url`</b> is a string containing the URL the client used to create the\n EventSource.\n* <b>`lastEventId`</b> is a string containing the last event ID received by the\n client. You can use this when the client reconnects after a dropped\n connection to determine which messages need resending.\n\nWhen you initialize an EventSource with ` new EventSource()`, you can pass\nconfiguration options after the `response` parameter. Available options are:\n\n* <b>`headers`</b> is an object containing custom headers to be set on the\n EventSource response.\n* <b>`retry`</b> is a number that tells the client how long (in seconds) it\n should wait after a dropped connection before attempting to reconnect.\n* <b>`ping`</b> is a number that tells the server how often (in seconds) to\n send 'ping' packets to the client to keep the connection open, to defeat\n timeouts set by proxies. The client will ignore these messages.\n\nFor example, this creates a connection that allows access from any origin, pings\nevery 15 seconds and is retryable every 10 seconds if the connection is broken:\n\n```js\nvar es = new EventSource(request, response, {\n headers: {'Access-Control-Allow-Origin': '*'},\n ping: 15,\n retry: 10\n});\n```\n\nYou can send a ping message at any time by calling `es.ping()`. Unlike\nWebSocket, the client does not send a response to this; it is merely to send\nsome data over the wire to keep the connection alive.\n\n\n## License\n\n(The MIT License)\n\nCopyright (c) 2010-2013 James Coglan\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the 'Software'), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\nof the Software, and to permit persons to whom the Software is furnished to do\nso, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n",
"readmeFilename": "README.md",
"_id": "faye-websocket@0.7.0",
"dist": {
"shasum": "c16c50ec0d483357a8eafd1ec6fcc313d027f5be"
},
"_from": "faye-websocket@0.7.0",
"_resolved": "https://registry.npmjs.org/faye-websocket/-/faye-websocket-0.7.0.tgz"
}