bf257c8 Nov 19, 2014
1 contributor

Users who have contributed to this file

51 lines (32 sloc) 1.91 KB


var W3CWebSocket = require('websocket').w3cwebsocket

Implementation of the W3C WebSocket API for browsers.

The exposed class lets the developer use the browser W3C WebSocket API in Node:

var WS = require('websocket').w3cwebsocket;

WS === window.WebSocket
// => true when in the browser

var ws = new WS('ws://', 'foo', '');
// - In Node it creates an instance of websocket.W3CWebSocket.
// - In the browser it creates an instance of window.WebSocket (third parameter
//   is ignored by the native WebSocket constructor).

ws.onopen = function() { console.log('ws open'); };
// etc.


new W3CWebSocket(requestUrl, requestedProtocols, [[[[origin], headers], requestOptions], clientConfig])

clientConfig is the parameter of the WebSocketClient constructor.

requestUrl, requestedProtocols, origin, headers and requestOptions are parameters to be used in the connect() method of WebSocketClient.

This constructor API makes it possible to use the W3C API and "browserify" the Node application into a valid browser library.

When running in a browser (for example by using browserify) the browser's native WebSocket implementation is used, and thus just the first and second arguments (requestUrl and requestedProtocols) are used (those allowed by the W3C WebSocket API).


  • bufferedAmount attribute is always 0.
  • binaryType is "arraybuffer" by default given that "blob" is not supported (Node does not implement the Blob class).
  • send() method allows arguments of type DOMString, ArrayBuffer, ArrayBufferView (Int8Array, etc) or Node Buffer, but does not allow Blob.