Get started with reconnecting-websocket CDN

MIT licensed

Library implementing WebSocket protocol with auto-reconnection: ReconnectingWebSocket.

Tags:
  • reconnecting
  • websocket

Stable version

Copied!

How to start using reconnecting-websocket CDN


// Import the library
const WebSocket = require('ws');
const ReconnectingWebSocket = require('reconnecting-websocket');

// Create a new ReconnectingWebSocket instance
const ws = new ReconnectingWebSocket('wss://your-websocket-url.com', [], {
  // Customize the options if needed
  maxRetries: 10,
  retryDelay: 5000,
  timeout: 3000,
});

// Connection opened
ws.onopen = () => {
  console.log('WebSocket connection opened.');
};

// Connection closed
ws.onclose = () => {
  console.log('WebSocket connection closed.');
};

// Message received
ws.onmessage = (event) => {
  console.log('Received message:', event.data);
};

// Send a message to the server
const message = 'Hello, WebSocket!';
ws.send(message);
Copied!
Copied!

All versions