https://github.com/dimdengd/recowebsocket
WebSocket with automatic reconnection.
https://github.com/dimdengd/recowebsocket
Last synced: 11 months ago
JSON representation
WebSocket with automatic reconnection.
- Host: GitHub
- URL: https://github.com/dimdengd/recowebsocket
- Owner: dimdenGD
- License: mit
- Created: 2024-12-28T00:23:13.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-28T22:45:27.000Z (over 1 year ago)
- Last Synced: 2025-06-09T06:16:48.432Z (12 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RecoWebSocket
WebSocket with automatic reconnection. Just a wrapper around `ws` Client with a bunch of new options.
## Installation
```bash
npm install recowebsocket
```
## Usage
Everything is the same as in `ws` Client, but with `reconnect`, `reconnectInterval`, `reconnectMaxAttempts` options. All events you attached will be automatically attached to the underlying `ws` Client, even on reconnect. Set `reconnectMaxAttempts` to `-1` to reconnect indefinitely.
```js
import WebSocket from "recowebsocket";
const ws = new WebSocket("wss://echo.websocket.org", {
// these are the default values
reconnect: true,
reconnectInterval: 1000,
reconnectMaxAttempts: -1,
});
ws.on("open", () => {
console.log("Connected to the server");
ws.send("Hello, server!");
});
```
Calling `ws.close()` or `ws.terminate()` will close the underlying `ws` Client and stop reconnecting.
There's also additional nice method `ws.json(data, options)`, which will stringify data and send it as a JSON.
## Caveats
- Not all event emitter methods are supported. Only `on`, `once` and `off` are supported. `once` will be called once on each reconnect. So it's not actually once I guess.
- Some obscure ws client stuff might not work.
## License
MIT