Giter Club home page Giter Club logo

boomerang-socket's Introduction

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

name type default description
connectTimeout number 250 millisecond timeout before a new connection must return open
reconnect boolean true enables or disables auto-reconnection
reconnectAttempts number Infinity maximum number of reconnection attempts
reconnectDelay number 3000 millisecond delay until first reconnection attempt
reconnectDelayExponent number 1.05 exponent applied to reconnectDelay between each reconnection attempt

Contribute

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

boomerang-socket's People

Contributors

thegreatercurve avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mgtitimoli

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.