Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/z-hao-wang/websocket-reconnect
websocket auto reconnect wrapper
https://github.com/z-hao-wang/websocket-reconnect
javascript nodejs typescript websocket
Last synced: about 2 months ago
JSON representation
websocket auto reconnect wrapper
- Host: GitHub
- URL: https://github.com/z-hao-wang/websocket-reconnect
- Owner: z-hao-wang
- License: mit
- Created: 2019-04-04T21:29:10.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-17T20:12:35.000Z (8 months ago)
- Last Synced: 2024-12-06T08:38:23.690Z (2 months ago)
- Topics: javascript, nodejs, typescript, websocket
- Language: TypeScript
- Size: 28.3 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: license
Awesome Lists containing this project
README
# websocket reconnect
With typing and data caching
## Getting Started
This repo will create a websocket client that auto retry to reconnect to the sever
It will also cache the messages that failed to send and send after reconnect success.```
npm i websocket-reconnect --saveimport { WsReconnect } from '../src/WsReconnect';
const ws = new WsReconnect({ reconnectDelay: 5000 });
ws.open(`ws://${host}:${port}`);ws.on('open', function open() {
// this will only be called once, not on reconnect
});ws.on('reconnect', function open() {
// this will only be called on every reconnect attempt
});ws.on('message', (data: string) => {
const json = JSON.parse(data);
console.log('======== received', json);
});ws.on('close', () => {
interval && clearInterval(interval);
});
```
Please refer the examples for more details## Testing with examples
```
npm ci
npm run tsc
# run this in terminal #1
node dist/example/WsServer.js
# run this in terminal #2
node dist/example/WsClient.js
```Then ctrl + c to exist terminal #1, watch the terminal #2 WsClient to auto reconnect, once WsServer is restarted, the WsClient can rebuild connect.