kiejo / robust-websocket

A robust reconnecting WebSocket client for the browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

robust-websocket

A robust, reconnecting WebSocket client for the browser

SauceLabs Test Status

robust-websocket is a wrapper around the standard WebSocket class that implements the same interface, but can reconnect when disconnected or the user's computer comes back online.

It is error-code aware and will not reconnect on 1008 (HTTP 400 equivalent) and 1011 (HTTP 500 equivalent) by default. This behavior is fully configurable via the shouldConnect (see Usage).

  • Tests! You know it works like stated and regressions will be caught.
  • Is aware of online and offline, and won't burn up the users battery and CPU reconnected when offline, and will reconnect when it is online again.
  • Natively aware of error codes
  • Any kind of reconnect strategy is possible via functional composition

Usage

CodePen Example

Use it as you would a normal websocket:

var ws = new RobustWebSocket('ws://echo.websocket.org/')

ws.addEventListener('open', function(event) {
  ws.send('Hello!')
})

ws.addEventListener('message', function(event) {
  console.log('we got: ' + event.data)
})

But with an optional set of options you can specify as a 3rd parameter

var ws = new RobustWebSocket('ws://echo.websocket.org/', null {
   // The number of milliseconds to wait before a connection is considered to have timed out. Defaults to 4 seconds.
   timeout: 4000,
  // A function that given a CloseEvent or an online event (https://developer.mozilla.org/en-US/docs/Online_and_offline_events) and the `RobustWebSocket`,
  // will return the number of milliseconds to wait to reconnect, or a non-Number to not reconnect.
  // see below for more examples; below is the default functionality.
  shouldReconnect: function(event, ws) {
    if (event.code === 1008 || event.code === 1011) return
    return [0, 3000, 10000][ws.attempts]
  },
  // A boolean indicating whether or not to open the connection automatically. Defaults to true, matching native [WebSocket] behavior.
  // You can open the websocket by calling `open()` when you are ready. You can close and re-open the RobustWebSocket instance as much as you wish.
  automaticOpen: true
})

shouldReconnect Examples

Reconnect with an exponetial backoff on all errors

function shouldReconnect(event, ws) {
  return Math.pow(1.5, ws.attempts) * 500
}

Reconnect immediately but only 20 times per RobustWebSocket instance

function shouldReconnect(event, ws) {
  return ws.reconnects <= 20 && 0
}

Reconnect only on some whitelisted codes, and only 3 attempts, except on online events, then connect immediately

function shouldReconnect(event, ws) {
  if (event.type === 'online') return 0
  return [1006,1011,1012].indexOf(event.code) && [1000,5000,10000][ws.attempt]
}

See documentation for CloseEvent and online event, the two types of events that shouldReconnect will receive.

Polyfills needed

You may need these polyfills to support older browsers

About

A robust reconnecting WebSocket client for the browser

License:ISC License


Languages

Language:JavaScript 91.4%Language:HTML 8.6%