Socket
Socket
Sign inDemoInstall

websocket-reconnect

Package Overview
Dependencies
5
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    websocket-reconnect

With typing and data caching


Version published
Weekly downloads
231
decreased by-62.13%
Maintainers
1
Install size
236 kB
Created
Weekly downloads
 

Readme

Source

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.

Keywords

FAQs

Last updated on 18 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc