Registry / communication / ws-reconnect

ws-reconnect

JSON →
library1.0.7jsnpmunverified

ws-reconnect is a lightweight Node.js WebSocket client with automatic reconnection logic. Version 1.0.7 is the current stable release, but the package has not been updated since 2019. It provides event-driven reconnection with configurable retry count and reconnect interval. Unlike more modern alternatives (e.g., reconnecting-websocket), this package is minimal, untyped, and does not support browser environments or the standard WebSocket API. It depends on the 'ws' library. Note: The constructor is named WSCLIENT (not WSCLINET as in the README example).

npm install ws-reconnect
INSTALL
IMPORT
SIG · WS-RECONNECT
W
ws-reconnect
communicationjavascriptv1.0.7
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const WSCLIENT = require('ws-reconnect');
const WSCLIENT = require('ws-reconnect').default;
CJS only; no ESM or TypeScript support. The exported constructor is named WSCLIENT (not WsReconnect or similar).
WSCLIENT
import WSCLIENT = require('ws-reconnect');
import WSCLIENT from 'ws-reconnect';
TypeScript requires `import = require` for CJS modules; default import does not work.
WebSocket
const WebSocket = require('ws');
const WebSocket = require('ws-reconnect');
ws-reconnect does not re-export the WebSocket class; you must require('ws') separately.

Shows how to create a WebSocket server and connect using ws-reconnect with reconnection options and event listeners.

const WebSocket = require('ws'); const WSCLIENT = require('ws-reconnect'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', (ws) => { ws.send('hello'); ws.on('message', (data) => console.log('received:', data)); }); const client = new WSCLIENT('localhost:8080', { retryCount: 3, reconnectInterval: 5 }); client.start(); client.on('connect', () => console.log('connected')); client.on('message', (data) => console.log(data)); client.on('reconnect', () => console.log('reconnecting')); client.on('destroyed', () => console.log('destroyed')); setTimeout(() => client.close(), 10000);
Debug
Known issues
gotchaConstructor name is WSCLIENT, not WsReconnect or wsclient. The README example has a typo ('WSCLINET').
fix
Use new WSCLIENT(url, opts) — note all caps, no typo.
affects: >=0.0.0
gotchaThe 'retryCount' option defaults to 2, but the total connection attempts are retryCount + 1 initial attempt? The documentation is unclear.
fix
Set retryCount to the desired number of reconnection attempts (initial connection not counted).
affects: >=0.0.0
gotchaThe package is CJS-only; no ESM exports. Cannot use 'import' syntax in Node.js without transpilation.
fix
Use require('ws-reconnect') or configure TypeScript with 'esModuleInterop' and 'import = require'.
affects: >=0.0.0
breakingNo TypeScript declarations; accessing .readyState or other WebSocket properties is not typed.
fix
Create a custom type declaration or use '@ts-ignore'.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'ws'
ws is a peer dependency but not automatically installed.
fix
Run 'npm install ws' alongside ws-reconnect.
TypeError: WSCLIENT is not a constructor
Using a wrong import/require pattern (e.g., import WSCLIENT from 'ws-reconnect' in an ESM context).
fix
Use 'const WSCLIENT = require("ws-reconnect")' in Node.js CJS.
Unhandled 'error' event
The internal ws instance emits errors that are not caught by ws-reconnect.
fix
Attach a 'error' event listener on the client object: client.on('error', (err) => console.error(err));
Upgrade
Version history
1.0.7latest on npm
Audit
Dependencies
wsrequiredThe underlying WebSocket implementation for Node.js.
Agent activity
53 hits · last 30 days
node
42
OpenAI (training)
1
Resources
ws-reconnect — npm install ws-reconnect · libregistry