1.0.10 • Published 4 months ago

websocket-reconnect v1.0.10

Weekly downloads
19
License
MIT
Repository
github
Last release
4 months ago

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 --save

import { 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.

1.0.10

4 months ago

1.0.9

4 months ago

1.0.8

6 months ago

1.0.7

1 year ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago