websocket-reconnector
v1.1.1
Published
A wrapper to W3C WebSocket objects to seamlesly add reconnection feature
Downloads
20
Readme
websocket-reconnector
A wrapper to W3C WebSocket objects to seamlesly add reconnection feature.
Installation
$ npm install --save websocket-reconnector
Usage
const WebSocketReconnector = require('websocket-reconnector')
// Wrap any W3C WebSocket implementation
const ReconnectingWebSocket = WebSocketReconnector(WebSocket)
// Create your WebSocket client as usual and attach events
const client = ReconnectingWebsocket('ws://localhost')
// On connection and one every reconnection, send a message
client.onopen(() => client.send('Hello server'))
// A response is always received, no need to resubscribe to events
client.onmessage(event => console.log(`Message received: ${event.data}`))
API
The API is fully compatible with the W3C WebSocket API.
In addition, the following additional method is available:
client.reconnect()
Closes the underlying websocket instance and initiates a new connection.
License
MIT