New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

boomerang-socket

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

boomerang-socket

A simple WebSocket wrapper with automatic reconnection


Version published
Maintainers
1
Created
Boomerang Socket

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.

FAQs

Package last updated on 09 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts