Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reconnecting-websocket

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reconnecting-websocket

Reconnecting WebSocket

  • 4.4.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
255K
increased by3.98%
Maintainers
1
Weekly downloads
 
Created

What is reconnecting-websocket?

The 'reconnecting-websocket' npm package provides a WebSocket client that automatically reconnects if the connection is lost. It is designed to be a drop-in replacement for the standard WebSocket, adding the ability to handle disconnections and reconnections seamlessly.

What are reconnecting-websocket's main functionalities?

Automatic Reconnection

This feature allows the WebSocket to automatically reconnect if the connection is lost. The code sample demonstrates how to create a new ReconnectingWebSocket instance and handle open, close, and message events.

const ReconnectingWebSocket = require('reconnecting-websocket');
const rws = new ReconnectingWebSocket('ws://example.com');
rws.addEventListener('open', () => {
  console.log('Connected');
});
rws.addEventListener('close', () => {
  console.log('Disconnected');
});
rws.addEventListener('message', (event) => {
  console.log('Message from server:', event.data);
});

Custom Reconnection Logic

This feature allows you to customize the reconnection logic by setting options such as connection timeout, maximum retries, and reconnection delays. The code sample demonstrates how to create a ReconnectingWebSocket instance with custom options.

const ReconnectingWebSocket = require('reconnecting-websocket');
const options = {
  connectionTimeout: 1000,
  maxRetries: 10,
  maxReconnectionDelay: 10000,
  minReconnectionDelay: 1000
};
const rws = new ReconnectingWebSocket('ws://example.com', [], options);
rws.addEventListener('open', () => {
  console.log('Connected');
});
rws.addEventListener('close', () => {
  console.log('Disconnected');
});
rws.addEventListener('message', (event) => {
  console.log('Message from server:', event.data);
});

Event Handling

This feature allows you to handle various WebSocket events such as open, close, message, and error. The code sample demonstrates how to add event listeners for these events.

const ReconnectingWebSocket = require('reconnecting-websocket');
const rws = new ReconnectingWebSocket('ws://example.com');
rws.addEventListener('open', () => {
  console.log('Connected');
});
rws.addEventListener('close', () => {
  console.log('Disconnected');
});
rws.addEventListener('message', (event) => {
  console.log('Message from server:', event.data);
});
rws.addEventListener('error', (error) => {
  console.error('WebSocket error:', error);
});

Other packages similar to reconnecting-websocket

Keywords

FAQs

Package last updated on 07 Feb 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc