Registry / messaging / reconnectable-websocket

reconnectable-websocket

JSON →
library0.1.10jsnpmunverified

A lightweight WebSocket wrapper providing automatic reconnection with exponential backoff, configurable intervals, max attempts, and custom timeout. Version 0.1.10 is the latest stable release. It supports both blob and arraybuffer binary types, optional debug logging, and can be set to reconnect on clean close or error. Differentiators: simple API, no external runtime dependencies, and fine-grained control over reconnection behavior.

npm install reconnectable-websocket
INSTALL
IMPORT
SIG · RECONNECTABLE-WEBS
R
reconnectable-websocket
messagingjavascriptv0.1.10
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.

ReconnectableWebSocket
import ReconnectableWebSocket from 'reconnectable-websocket'
import { ReconnectableWebSocket } from 'reconnectable-websocket'
This package uses a default export. Named import will not work.
ReconnectableWebSocket
const ReconnectableWebSocket = require('reconnectable-websocket').default
const ReconnectableWebSocket = require('reconnectable-websocket')
CommonJS requires .default because the package is ESM-first.
TypeScript types
// No official types; use @types/reconnectable-websocket or declare module
import type { ReconnectableWebSocket } from 'reconnectable-websocket'
The package does not ship TypeScript definitions. Use a custom declaration file.

Creates a WebSocket with automatic reconnection to echo.websocket.org, sends a message, and logs events.

import ReconnectableWebSocket from 'reconnectable-websocket'; const ws = new ReconnectableWebSocket('wss://echo.websocket.org', [], { debug: false, automaticOpen: true, reconnectInterval: 2000, maxReconnectInterval: 30000, reconnectDecay: 1.5, timeoutInterval: 5000, maxReconnectAttempts: 10, randomRatio: 3, binaryType: 'blob', reconnectOnError: true, reconnectOnCleanClose: false }); ws.onopen = () => { console.log('Connected'); ws.send('Hello'); }; ws.onmessage = (event) => { console.log('Received:', event.data); }; ws.onerror = (error) => { console.error('Error:', error); }; ws.onclose = (event) => { console.log('Closed:', event.code, event.reason); };
Debug
Known issues
gotchaDefault import is not named: import as default, not named.
fix
Use `import ReconnectableWebSocket from 'reconnectable-websocket'`
affects: >=0.1.0
gotchaCommonJS require requires .default: use `require('pkg').default`
fix
Use `const ReconnectableWebSocket = require('reconnectable-websocket').default`
affects: >=0.1.0
gotchaOptions object properties are mutable after instantiation; changing reconnectInterval after open may have no effect.
fix
Set all options during construction or call `ws.open()` after changing properties.
affects: >=0.1.0
gotchaSetting `maxReconnectAttempts` to a number does not reset the counter on successful reconnect; manual tracking needed.
fix
Listen for `onopen` to reset your own counter if needed.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: ReconnectableWebSocket is not a constructor
Using `import { ReconnectableWebSocket }` instead of default import.
fix
`import ReconnectableWebSocket from 'reconnectable-websocket'`
Uncaught ReferenceError: module is not defined
Using CommonJS `require` in an ES module context without .default.
fix
Use `const ReconnectableWebSocket = require('reconnectable-websocket').default`
WebSocket is already in CONNECTING state
Calling `ws.open()` while `automaticOpen: true` and already connecting.
fix
Set `automaticOpen: false` or check `ws.readyState` before calling `open()`.
Upgrade
Version history
0.1.10latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
20
OpenAI (training)
1
Resources
reconnectable-websocket — npm install reconnectable-websocket · libregistry