{
  "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"
  ],
  "version": "0.4.4",
  "engines": {
    "node": ">=0.4.0"
  },
  "main": "./lib/faye/websocket",
  "devDependencies": {
    "jsclass": "",
    "pace": ""
  },
  "scripts": {
    "test": "node spec/runner.js"
  },
  "bugs": "http://github.com/faye/faye-websocket-node/issues",
  "licenses": [
    {
      "type": "MIT",
      "url": "http://www.opensource.org/licenses/mit-license.php"
    }
  ],
  "repositories": [
    {
      "type": "git",
      "url": "git://github.com/faye/faye-websocket-node.git"
    }
  ],
  "readme": "# faye-websocket\n\n* Travis CI build: [<img src=\"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/), [client](http://faye.jcoglan.com/autobahn/clients/)\n\nThis is a robust, general-purpose WebSocket implementation extracted from the\n[Faye](http://faye.jcoglan.com) project. It provides classes for easily building\nWebSocket servers and clients in Node. It does not provide a server itself, but\nrather makes it easy to handle WebSocket connections within an existing\n[Node](http://nodejs.org/) application. It does not provide any abstraction\nother than the standard [WebSocket API](http://dev.w3.org/html5/websockets/).\n\nIt also provides an abstraction for handling [EventSource](http://dev.w3.org/html5/eventsource/)\nconnections, which are one-way connections that allow the server to push data to\nthe client. They are based on streaming HTTP responses and can be easier to\naccess via proxies than WebSockets.\n\nThe server-side socket can process [draft-75](http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75),\n[draft-76](http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76),\n[hybi-07](http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07)\nand later versions of the protocol. It selects protocol versions automatically,\nsupports both `text` and `binary` messages, and transparently handles `ping`,\n`pong`, `close` and fragmented messages.\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.addListener('upgrade', function(request, socket, head) {\n  var ws = new WebSocket(request, socket, head);\n  \n  ws.onmessage = function(event) {\n    ws.send(event.data);\n  };\n  \n  ws.onclose = function(event) {\n    console.log('close', event.code, event.reason);\n    ws = null;\n  };\n});\n\nserver.listen(8000);\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 message\nis received. It returns `true` iff a ping message was sent. If the client does\nnot support ping/pong, this method sends no data and returns `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.onopen = function(event) {\n  console.log('open');\n  ws.send('Hello, world!');\n};\n\nws.onmessage = function(event) {\n  console.log('message', event.data);\n};\n\nws.onclose = function(event) {\n  console.log('close', event.code, event.reason);\n  ws = null;\n};\n```\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, head, ['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.\nIf they cannot agree on a protocol to use, the client closes the connection.\n\n\n## WebSocket API\n\nThe WebSocket API consists of several event handlers and a method for sending\nmessages.\n\n* <b><tt>onopen</tt></b> fires when the socket connection is established. Event\n  has no attributes.\n* <b><tt>onerror</tt></b> fires when the connection attempt fails. Event has no\n  attributes.\n* <b><tt>onmessage</tt></b> fires when the socket receives a message. Event has\n  one attribute, <b><tt>data</tt></b>, which is either a `String` (for text\n  frames) or a `Buffer` (for binary frames).\n* <b><tt>onclose</tt></b> fires when either the client or the server closes the\n  connection. Event has two optional attributes, <b><tt>code</tt></b> and\n  <b><tt>reason</tt></b>, that expose the status code and message sent by the\n  peer that closed the connection.\n* <b><tt>send(message)</tt></b> accepts either a `String` or a `Buffer` and\n  sends a text or binary message over the connection to the other peer.\n* <b><tt>close(code, reason)</tt></b> closes the connection, sending the given\n  status code and reason text, both of which are optional.\n* <b><tt>protocol</tt></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.addListener('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.onclose = 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.write('Hello');\n    response.end();\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><tt>url</tt></b> is a string containing the URL the client used to create\n  the EventSource.\n* <b><tt>lastEventId</tt></b> is a string containing the last event ID\n  received by the client. You can use this when the client reconnects after a\n  dropped 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><tt>retry</tt></b> is a number that tells the client how long (in seconds)\n  it should wait after a dropped connection before attempting to reconnect.\n* <b><tt>ping</tt></b> is a number that tells the server how often (in seconds)\n  to 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 pings every 15 seconds and is\nretryable every 10 seconds if the connection is broken:\n\n```js\nvar es = new EventSource(request, response, {ping: 15, retry: 10});\n```\n\nYou can send a ping message at any time by calling `es.ping()`. Unlike WebSocket,\nthe client does not send a response to this; it is merely to send some data over\nthe wire to keep the connection alive.\n\n\n## License\n\n(The MIT License)\n\nCopyright (c) 2009-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 use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell copies of the\nSoftware, and to permit persons to whom the Software is furnished to do so,\nsubject 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, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n",
  "readmeFilename": "README.markdown",
  "_id": "faye-websocket@0.4.4",
  "_from": "faye-websocket@~0.4.3"
}
