3 * Travis CI build: [![Build
4 status](https://secure.travis-ci.org/faye/faye-websocket-node.svg)](http://travis-ci.org/faye/faye-websocket-node)
5 * Autobahn tests: [server](http://faye.jcoglan.com/autobahn/servers/),
6 [client](http://faye.jcoglan.com/autobahn/clients/)
8 This is a general-purpose WebSocket implementation extracted from the
9 [Faye](http://faye.jcoglan.com) project. It provides classes for easily building
10 WebSocket servers and clients in Node. It does not provide a server itself, but
11 rather makes it easy to handle WebSocket connections within an existing
12 [Node](http://nodejs.org/) application. It does not provide any abstraction
13 other than the standard [WebSocket API](http://dev.w3.org/html5/websockets/).
15 It also provides an abstraction for handling
16 [EventSource](http://dev.w3.org/html5/eventsource/) connections, which are
17 one-way connections that allow the server to push data to the client. They are
18 based on streaming HTTP responses and can be easier to access via proxies than
25 $ npm install faye-websocket
29 ## Handling WebSocket connections in Node
31 You can handle WebSockets on the server side by listening for HTTP Upgrade
32 requests, and creating a new socket for the request. This socket object exposes
33 the usual WebSocket methods for receiving and sending messages. For example this
34 is how you'd implement an echo server:
37 var WebSocket = require('faye-websocket'),
38 http = require('http');
40 var server = http.createServer();
42 server.on('upgrade', function(request, socket, body) {
43 if (WebSocket.isWebSocket(request)) {
44 var ws = new WebSocket(request, socket, body);
46 ws.on('message', function(event) {
50 ws.on('close', function(event) {
51 console.log('close', event.code, event.reason);
60 `WebSocket` objects are also duplex streams, so you could replace the
61 `ws.on('message', ...)` line with:
67 Note that under certain circumstances (notably a draft-76 client connecting
68 through an HTTP proxy), the WebSocket handshake will not be complete after you
69 call `new WebSocket()` because the server will not have received the entire
70 handshake from the client yet. In this case, calls to `ws.send()` will buffer
71 the message in memory until the handshake is complete, at which point any
72 buffered messages will be sent to the client.
74 If you need to detect when the WebSocket handshake is complete, you can use the
77 If the connection's protocol version supports it, you can call `ws.ping()` to
78 send a ping message and wait for the client's response. This method takes a
79 message string, and an optional callback that fires when a matching pong message
80 is received. It returns `true` if and only if a ping message was sent. If the
81 client does not support ping/pong, this method sends no data and returns
85 ws.ping('Mic check, one, two', function() {
86 // fires when pong is received
91 ## Using the WebSocket client
93 The client supports both the plain-text `ws` protocol and the encrypted `wss`
94 protocol, and has exactly the same interface as a socket you would use in a web
95 browser. On the wire it identifies itself as `hybi-13`.
98 var WebSocket = require('faye-websocket'),
99 ws = new WebSocket.Client('ws://www.example.com/');
101 ws.on('open', function(event) {
103 ws.send('Hello, world!');
106 ws.on('message', function(event) {
107 console.log('message', event.data);
110 ws.on('close', function(event) {
111 console.log('close', event.code, event.reason);
116 The WebSocket client also lets you inspect the status and headers of the
117 handshake response via its `statusCode` and `headers` properties.
119 To connect via a proxy, set the `proxy` option to the HTTP origin of the proxy,
120 including any authorization information, custom headers and TLS config you
121 require. Only the `origin` setting is required.
124 var ws = new WebSocket.Client('ws://www.example.com/', [], {
126 origin: 'http://username:password@proxy.example.com',
127 headers: {'User-Agent': 'node'},
128 tls: {cert: fs.readFileSync('client.crt')}
133 The `tls` value is a Node 'TLS options' object that will be passed to
134 [`tls.connect()`](http://nodejs.org/api/tls.html#tls_tls_connect_options_callback).
137 ## Subprotocol negotiation
139 The WebSocket protocol allows peers to select and identify the application
140 protocol to use over the connection. On the client side, you can set which
141 protocols the client accepts by passing a list of protocol names when you
142 construct the socket:
145 var ws = new WebSocket.Client('ws://www.example.com/', ['irc', 'amqp']);
148 On the server side, you can likewise pass in the list of protocols the server
149 supports after the other constructor arguments:
152 var ws = new WebSocket(request, socket, body, ['irc', 'amqp']);
155 If the client and server agree on a protocol, both the client- and server-side
156 socket objects expose the selected protocol through the `ws.protocol` property.
159 ## Protocol extensions
161 faye-websocket is based on the
162 [websocket-extensions](https://github.com/faye/websocket-extensions-node)
163 framework that allows extensions to be negotiated via the
164 `Sec-WebSocket-Extensions` header. To add extensions to a connection, pass an
165 array of extensions to the `:extensions` option. For example, to add
166 [permessage-deflate](https://github.com/faye/permessage-deflate-node):
169 var deflate = require('permessage-deflate');
171 var ws = new WebSocket(request, socket, body, [], {extensions: [deflate]});
175 ## Initialization options
177 Both the server- and client-side classes allow an options object to be passed in
178 at initialization time, for example:
181 var ws = new WebSocket(request, socket, body, protocols, options);
182 var ws = new WebSocket.Client(url, protocols, options);
185 `protocols` is an array of subprotocols as described above, or `null`.
186 `options` is an optional object containing any of these fields:
188 * `extensions` - an array of
189 [websocket-extensions](https://github.com/faye/websocket-extensions-node)
190 compatible extensions, as described above
191 * `headers` - an object containing key-value pairs representing HTTP headers to
192 be sent during the handshake process
193 * `maxLength` - the maximum allowed size of incoming message frames, in bytes.
194 The default value is `2^26 - 1`, or 1 byte short of 64 MiB.
195 * `ping` - an integer that sets how often the WebSocket should send ping frames,
198 The client accepts some additional options:
200 * `proxy` - settings for a proxy as described above
201 * `tls` - a Node 'TLS options' object containing TLS settings for the origin
202 server, this will be passed to
203 [`tls.connect()`](http://nodejs.org/api/tls.html#tls_tls_connect_options_callback)
204 * `ca` - (legacy) a shorthand for passing `{tls: {ca: value}}`
209 Both server- and client-side `WebSocket` objects support the following API.
211 * <b>`on('open', function(event) {})`</b> fires when the socket connection is
212 established. Event has no attributes.
213 * <b>`on('message', function(event) {})`</b> fires when the socket receives a
214 message. Event has one attribute, <b>`data`</b>, which is either a `String`
215 (for text frames) or a `Buffer` (for binary frames).
216 * <b>`on('error', function(event) {})`</b> fires when there is a protocol error
217 due to bad data sent by the other peer. This event is purely informational,
218 you do not need to implement error recover.
219 * <b>`on('close', function(event) {})`</b> fires when either the client or the
220 server closes the connection. Event has two optional attributes, <b>`code`</b>
221 and <b>`reason`</b>, that expose the status code and message sent by the peer
222 that closed the connection.
223 * <b>`send(message)`</b> accepts either a `String` or a `Buffer` and sends a
224 text or binary message over the connection to the other peer.
225 * <b>`ping(message, function() {})`</b> sends a ping frame with an optional
226 message and fires the callback when a matching pong is received.
227 * <b>`close(code, reason)`</b> closes the connection, sending the given status
228 code and reason text, both of which are optional.
229 * <b>`version`</b> is a string containing the version of the `WebSocket`
230 protocol the connection is using.
231 * <b>`protocol`</b> is a string (which may be empty) identifying the subprotocol
235 ## Handling EventSource connections in Node
237 EventSource connections provide a very similar interface, although because they
238 only allow the server to send data to the client, there is no `onmessage` API.
239 EventSource allows the server to push text messages to the client, where each
240 message has an optional event-type and ID.
243 var WebSocket = require('faye-websocket'),
244 EventSource = WebSocket.EventSource,
245 http = require('http');
247 var server = http.createServer();
249 server.on('request', function(request, response) {
250 if (EventSource.isEventSource(request)) {
251 var es = new EventSource(request, response);
252 console.log('open', es.url, es.lastEventId);
254 // Periodically send messages
255 var loop = setInterval(function() { es.send('Hello') }, 1000);
257 es.on('close', function() {
263 // Normal HTTP request
264 response.writeHead(200, {'Content-Type': 'text/plain'});
265 response.end('Hello');
272 The `send` method takes two optional parameters, `event` and `id`. The default
273 event-type is `'message'` with no ID. For example, to send a `notification`
277 es.send('Breaking News!', {event: 'notification', id: '99'});
280 The `EventSource` object exposes the following properties:
282 * <b>`url`</b> is a string containing the URL the client used to create the
284 * <b>`lastEventId`</b> is a string containing the last event ID received by the
285 client. You can use this when the client reconnects after a dropped connection
286 to determine which messages need resending.
288 When you initialize an EventSource with ` new EventSource()`, you can pass
289 configuration options after the `response` parameter. Available options are:
291 * <b>`headers`</b> is an object containing custom headers to be set on the
292 EventSource response.
293 * <b>`retry`</b> is a number that tells the client how long (in seconds) it
294 should wait after a dropped connection before attempting to reconnect.
295 * <b>`ping`</b> is a number that tells the server how often (in seconds) to send
296 'ping' packets to the client to keep the connection open, to defeat timeouts
297 set by proxies. The client will ignore these messages.
299 For example, this creates a connection that allows access from any origin, pings
300 every 15 seconds and is retryable every 10 seconds if the connection is broken:
303 var es = new EventSource(request, response, {
304 headers: {'Access-Control-Allow-Origin': '*'},
310 You can send a ping message at any time by calling `es.ping()`. Unlike
311 WebSocket, the client does not send a response to this; it is merely to send
312 some data over the wire to keep the connection alive.
319 Copyright (c) 2010-2015 James Coglan
321 Permission is hereby granted, free of charge, to any person obtaining a copy of
322 this software and associated documentation files (the 'Software'), to deal in
323 the Software without restriction, including without limitation the rights to
324 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
325 the Software, and to permit persons to whom the Software is furnished to do so,
326 subject to the following conditions:
328 The above copyright notice and this permission notice shall be included in all
329 copies or substantial portions of the Software.
331 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
332 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
333 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
334 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
335 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
336 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.