https://github.com/autonomoussoftware/websocket-reconnector
A wrapper to W3C WebSocket objects to seamlesly add reconnection feature
https://github.com/autonomoussoftware/websocket-reconnector
Last synced: about 1 year ago
JSON representation
A wrapper to W3C WebSocket objects to seamlesly add reconnection feature
- Host: GitHub
- URL: https://github.com/autonomoussoftware/websocket-reconnector
- Owner: autonomoussoftware
- License: mit
- Created: 2018-09-05T20:19:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-06T13:48:24.000Z (about 6 years ago)
- Last Synced: 2025-02-25T12:41:15.984Z (over 1 year ago)
- Language: JavaScript
- Size: 84 KB
- Stars: 1
- Watchers: 10
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# websocket-reconnector
[](https://travis-ci.com/autonomoussoftware/websocket-reconnector)
A wrapper to W3C WebSocket objects to seamlesly add reconnection feature.
## Installation
```bash
$ npm install --save websocket-reconnector
```
## Usage
```js
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](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) API.
In addition, the following additional method is available:
#### `client.reconnect()`
Closes the underlying websocket instance and initiates a new connection.
## License
MIT