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://websockets.github.com/ws for the full reports.
disableHixieoption to true.)
npm install --save 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; ++iarrayi = 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
var WebSocketServer = require'ws'Serverwss = port: 8080 ;wsson'connection'wson'message'console.log'received: %s' message;;wssend'something';;
var server = require'http'createServerurl = require'url'WebSocketServer = require'ws'Serverwss = server: serverexpress = require'express'app = expressport = 4080;appuseressend msg: "hello" ;;wsson'connection'var location = urlparsewsupgradeRequrl true;// you might use location.query.access_token to authenticate or share sessions// or ws.upgradeReq.headers.cookie (see )wson'message'console.log'received: %s' message;;wssend'something';;serveron'request' app;serverlistenport console.log'Listening on ' + serveraddressport ;
var WebSocketServer = require'ws'Serverwss = port: 8080 ;wssbroadcast =wssclientsforEachclientsenddata;;;
// 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 not defined, 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.try wssend'something';catch e /* handle error */
var WebSocket = require'ws';var ws = 'ws://echo.websocket.org/'protocolVersion: 8origin: '';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;;
When including ws via a browserify bundle, ws returns global.WebSocket which has slightly different API. You should use the standard WebSockets API instead.
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.
/doc/ws.md for Node.js-like docs for the ws classes.
We're using the GitHub
releases for changelog entries.
(The MIT License)
Copyright (c) 2011 Einar Otto Stangvik <email@example.com>
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.