A simple WebSocket wrapper with automatic reconnection
npm install boomerang-socket
 
Boomerang Socket is a small JavaScript library that extends the native WebSocket API to allow automatic reconnection if a client is disconnected.
- 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.
```
npm install boomerang-socket
``
npm run test
It can be used in exactly the same way as a native WebSocket, with exactly the same API.
`js
// 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.
`js`
const socket = new BoomerangSocket("wss://localhost:8080", undefined, {
reconnectAttempts: 25,
reconnectDelay: 5000,
});
| name | type | default | description |
|------------------------|---------|------------|------------------------------------------------------------------------|
| connectTimeout | number | 250 | millisecond timeout before a new connection must return open |true
| reconnect | boolean | | enables or disables auto-reconnection |Infinity
| reconnectAttempts | number | | maximum number of reconnection attempts |3000
| reconnectDelay | number | | millisecond delay until first reconnection attempt |1.05
| reconnectDelayExponent | number | | exponent applied to reconnectDelay` between each reconnection attempt |
We actively welcome any contributions. You can open a Pull Request here.