Websocket Client & Server Library implementing the WebSocket protocol as specified in RFC 6455.
For a WebSocket client written in ActionScript 3, see my AS3WebScocket project.
Current Version: 1.0.18 — Released 2015-03-19
All current browsers are fully supported.
Safari older than 6.0 is not supported since it uses a very old draft of WebSockets
If you need to simultaneously support legacy browser versions that had implemented draft-75/draft-76/draft-00, take a look here: https://gist.github.com/1428579
There are some basic benchmarking sections in the Autobahn test suite. I've put up a benchmark page that shows the results from the Autobahn tests run against AutobahnServer 0.4.10, WebSocket-Node 1.0.2, WebSocket-Node 1.0.4, and ws 0.3.4.
The very complete Autobahn Test Suite is used by most WebSocket implementations to test spec compliance and interoperability.
This library has been used in production on worlize.com since April 2011 and seems to be stable. Your mileage may vary.
Tested with the following node versions:
It may work in earlier or later versions but I'm not actively testing it outside of the listed versions. YMMV.
A few users have reported difficulties building the native extensions without first manually installing node-gyp. If you have trouble building the native extensions, make sure you've got a C++ compiler, and have done
npm install -g node-gyp first.
Native extensions are optional, however, and WebSocket-Node will work even if the extensions cannot be compiled.
In your project root:
$ npm install websocket
Then in your code:
var WebSocketServer = require'websocket'server;var WebSocketClient = require'websocket'client;var WebSocketFrame = require'websocket'frame;var WebSocketRouter = require'websocket'router;var W3CWebSocket = require'websocket'w3cwebsocket;
Because there is a small C++ component used for validating UTF-8 data, you will need to install a few other software packages in addition to Node to be able to build this module:
Here's a short example showing a server that echos back anything sent to it, whether utf-8 or binary.
#!/usr/bin/env nodevar WebSocketServer = require'websocket'server;var http = require'http';var server = httpcreateServerconsole.log + ' Received request for ' + requesturl;responsewriteHead404;responseend;;serverlisten8080console.log + ' Server is listening on port 8080';;wsServer =httpServer: server// You should not use autoAcceptConnections for production// applications, as it defeats all standard cross-origin protection// facilities built into the protocol and the browser. You should// *always* verify the connection's origin and decide whether or not// to accept it.autoAcceptConnections: false;// put logic here to detect whether the specified origin is allowed.return true;wsServeron'request'if !originIsAllowedrequestorigin// Make sure we only accept requests from an allowed originrequestreject;console.log + ' Connection from origin ' + requestorigin + ' rejected.';return;var connection = requestaccept'echo-protocol' requestorigin;console.log + ' Connection accepted.';connectionon'message'if messagetype === 'utf8'console.log'Received Message: ' + messageutf8Data;connectionsendUTFmessageutf8Data;else if messagetype === 'binary'console.log'Received Binary Message of ' + messagebinaryDatalength + ' bytes';connectionsendBytesmessagebinaryData;;connectionon'close'console.log + ' Peer ' + connectionremoteAddress + ' disconnected.';;;
This is a simple example client that will print out any utf-8 messages it receives on the console, and periodically sends a random number.
This code demonstrates a client in Node.js, not in the browser
#!/usr/bin/env nodevar WebSocketClient = require'websocket'client;var client = ;clienton'connectFailed'console.log'Connect Error: ' + errortoString;;clienton'connect'console.log'WebSocket Client Connected';connectionon'error'console.log"Connection Error: " + errortoString;;connectionon'close'console.log'echo-protocol Connection Closed';;connectionon'message'if messagetype === 'utf8'console.log"Received: '" + messageutf8Data + "'";;if connectionconnectedvar number = MathroundMathrandom * 0xFFFFFF;connectionsendUTFnumbertoString;setTimeoutsendNumber 1000;sendNumber;;clientconnect'ws://localhost:8080/' 'echo-protocol';
Same example as above but using the W3C WebSocket API.
var W3CWebSocket = require'websocket'w3cwebsocket;var client = 'ws://localhost:8080/' 'echo-protocol';console.log'Connection Error';;console.log'WebSocket Client Connected';if clientreadyState === clientOPENvar number = MathroundMathrandom * 0xFFFFFF;clientsendnumbertoString;setTimeoutsendNumber 1000;sendNumber;;console.log'echo-protocol Client Closed';;if typeof edata === 'string'console.log"Received: '" + edata + "'";;
For an example of using the request router, see
libwebsockets-test-server.js in the
A presentation on the state of the WebSockets protocol that I gave on July 23, 2011 at the LA Hacker News meetup. WebSockets: The Real-Time Web, Delivered