simple to use, blazing fast and thoroughly tested websocket client, server and console for node.js, up-to-date against RFC-6455
ws is a simple to use websocket implementation, up-to-date against RFC-6455, and probably the fastest WebSocket library for node.js.
Passes the quite extensive Autobahn test suite. See http://einaros.github.com/ws for the full reports.
Comes with a command line utility,
wscat, which can either act as a server (--listen), or client (--connect); Use it to debug simple websocket services.
disableHixieoption to true.)
protocolVersion: 8, or argument
-p 8for wscat)
protocolVersion: 13, or argument
-p 13for wscat)
See the echo.websocket.org example below for how to use the
npm install ws
var WebSocket = require'ws';var ws = 'ws://www.host.com/path';wson'open'wssend'something';;wson'message'// flags.binary will be set if a binary data is received// flags.masked will be set if the data was masked;
var WebSocket = require'ws';var ws = 'ws://www.host.com/path';wson'open'var array = 5;for var i = 0; i < arraylength; ++i arrayi = i / 2;wssendarray binary: true mask: true;;
mask, as done for the send options above, will cause the data to be masked according to the websocket protocol. The same option applies for text data.
var WebSocketServer = require'ws'Serverwss = port: 8080;wsson'connection'wson'message'console.log'received: %s' message;;wssend'something';;
// If the WebSocket is closed before the following send is attemptedwssend'something';// Errors (both immediate and async write errors) can be detected in an optional callback.// The callback is also the only way of being notified that data has actually been sent.wssend'something'// if error is null, the send has been completed,// otherwise the error object will indicate what failed.;// Immediate errors can also be handled with try/catch-blocks, but **note**// that since sends are inherently asynchronous, socket write failures will *not*// be captured when this technique is used.trywssend'something';catch e// handle error
var WebSocket = require'ws';var ws = 'ws://echo.websocket.org/' protocolVersion: 8 origin: '';wson'open'console.log'connected';wssendDatenowtoString mask: true;;wson'close'console.log'disconnected';;wson'message'console.log'Roundtrip time: ' + Datenow - parseIntdata + 'ms' flags;setTimeoutwssendDatenowtoString mask: true;500;;
$ npm install -g ws $ wscat -c ws://echo.websocket.org -p 8 connected (press CTRL+C to quit) > hi there < hi there > are you a happy parrot? < are you a happy parrot?
For a full example with a browser client communicating with a ws server, see the examples folder.
Note that the usage together with Express 3.0 is quite different from Express 2.x. The difference is expressed in the two different serverstats-examples.
Otherwise, see the test cases.
See the doc/ directory for Node.js-like docs for the ws classes.
(The MIT License)
Copyright (c) 2011 Einar Otto Stangvik <firstname.lastname@example.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.