0.3.1 • Published 6 years ago

boomerang-socket v0.3.1

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Boomerang Socket

CircleCI Coverage Status

Boomerang Socket is a small JavaScript library that extends the native WebSocket API to allow automatic reconnection if a client is disconnected.

Features

  • Fully mirrors the native WebSocket API.
  • Automatic reconnection is RFC compliant.
  • Only 7kb when minified.
  • Compatible with IE11 and upwards.
  • Fully tested.
  • No external dependencies.

Installation

npm install boomerang-socket

Tests

npm run test

Usage

It can be used in exactly the same way as a native WebSocket, with exactly the same API.

// ES2015
import BoomerangSocket from "boomerang-socket";

// Or CommonJS
const BoomerangSocket = require("boomerang-socket");

const socket = new BoomerangSocket("wss://localhost:5000");

socket.addEventListener("open", (event) => {
  socket.send("Hello server!");
});

socket.onmessage = (event) =>  {
  console.log("Message from server: ", event.data);
};

The only difference is that the BoomerangSocket constructor accepts an additional third argument: an options config object which customises the auto-reconnect behaviour.

const socket = new BoomerangSocket("wss://localhost:8080", undefined, {
  reconnectAttempts: 25,
  reconnectDelay: 5000,
});

Options

nametypedefaultdescription
connectTimeoutnumber250millisecond timeout before a new connection must return open
reconnectbooleantrueenables or disables auto-reconnection
reconnectAttemptsnumberInfinitymaximum number of reconnection attempts
reconnectDelaynumber3000millisecond delay until first reconnection attempt
reconnectDelayExponentnumber1.05exponent applied to reconnectDelay between each reconnection attempt

Contribute

We actively welcome any contributions. You can open a Pull Request here.