ReWebSocket is 99% API-compatible with WebSocket:
var WebSocket = require('ws');
var ReWebSocket = require('rewebsocket')(WebSocket);
var ws = new ReWebSocket("ws://127.0.0.1:3000");
ws.open(); // the only big difference: need to call open explicitly!
ws.onmessage = function(event) {
console.log("Received %j", event.data);
};
ws.onopen = function(event) {
console.log("Connected");
};
ws.onclose = function(event) {
if (event.previousReadyState == WebSocket.OPEN)
console.log("Disconnected");
else
console.log("Connection failed.");
};
ws.onerror = function(event) {
// in most cases, you only want to handle this event for logging purposes
// console.log("Error: %s", event.message);
};
ws.onconnecting = function(event) {
console.log("Connecting...");
};
npm install rewebsocket
Also works as a client-side module via Browserify.
Method differences:
- You need to call
open
explicitly to start connection. - You can call
open
andclose
multiple times; after you callopen
, ReWebSocket will try to keep the connection open until you callclose
. - Additional
reconnect
method closes and reopens the connection.
Event differences:
- In
onclose
,readyState
will beWebSocket.CONNECTING
if a reconnection attempt is pending, andWebSocket.CLOSED
after an explicitclose
call. - In
onclose
, additionalpreviousReadyState
property is provided. You can use it to distinguish failed connection attempts from aborted connections, as demonstrated by the example above. - Additional
onconnecting
event is fired before each connection attempt.
Properties:
timeoutInterval
— connectiont timeout in milliseconds, defaults to 10000.reconnectIntervals
— array of delays between reconnection attempts, in milliseconds; defaults to [10, 100, 500, 1000, 2000, 3000, 5000, 8000, 13000, 21000, 34000, 55000, 60000]; use this to specify your preferred back-off scheme; after a successful connection, the first value is used again; the last value is the maximum interval.
node example/example.js
For additional logging, try:
DEBUG=rewebsocket node example/example.js
© 2012 Andrey Tarantsov andrey@tarantsov.com.
Provided under the MIT license.