Registry / messaging / robust-websocket

robust-websocket

JSON →
library1.0.0jsnpmunverified

A robust, reconnecting WebSocket client for the browser that wraps the standard WebSocket class with the same interface but adds automatic reconnection. It is error-code aware (avoids reconnecting on 1008/1011 by default) and respects browser online/offline events to prevent unnecessary reconnection attempts. Version 1.0.0 is stable; well-tested with cross-browser coverage. Key differentiators vs alternatives: includes test suite, offline awareness, and functional composition for custom reconnect strategies.

npm install robust-websocket
INSTALL
IMPORT
SIG · ROBUST-WEBSOCKET
R
robust-websocket
messagingjavascriptv1.0.0
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.

RobustWebSocket
import RobustWebSocket from 'robust-websocket'
const { RobustWebSocket } = require('robust-websocket')
Default export: RobustWebSocket is the constructor. In CJS, use const RobustWebSocket = require('robust-websocket'); named destructure won't work.
RobustWebSocket
const RobustWebSocket = require('robust-websocket')
import { RobustWebSocket } from 'robust-websocket'
This package is CommonJS; using named ES import will fail. Use default import or require.
RobustWebSocket
const RobustWebSocket = require('robust-websocket')
const RobustWebSocket = require('robust-websocket').RobustWebSocket
The module exports the constructor directly, not as a property of an object.

Creates a RobustWebSocket instance, listens for open/message/close events, sends a message, and logs output.

const RobustWebSocket = require('robust-websocket'); const ws = new RobustWebSocket('wss://echo.websocket.org'); ws.addEventListener('open', () => { console.log('connected'); ws.send('Hello!'); }); ws.addEventListener('message', (event) => { console.log('Received:', event.data); ws.close(); }); ws.addEventListener('close', (event) => { console.log('Disconnected, code:', event.code); });
Debug
Known issues
gotchaDo not instantiate RobustWebSocket without 'new' – it will throw TypeError because it's a constructor.
fix
Always use 'new RobustWebSocket(url)' or 'new RobustWebSocket(url, protocols, options)'
affects: >=0.0.0
gotchaThe 'shouldReconnect' option receives a CloseEvent or an online event, not a raw Event. The online event has 'type' === 'online' and no 'code' property. Attempting to access 'event.code' on an online event may return undefined.
fix
Check event.type before accessing event.code: if (event.type === 'online') { /* handle online */ } else { /* handle CloseEvent */ }
affects: >=0.0.0
deprecatedThe option 'maxReconnectAttempts' does not exist; use shouldReconnect to limit attempts.
fix
Use shouldReconnect to return undefined or non-number after max attempts.
affects: >=0.0.0
gotchaThe 'attempts' property starts at 0 and increments on each reconnection attempt. The 'reconnects' property counts successful reconnections. They are different.
fix
Use ws.attempts in shouldReconnect for attempt index; use ws.reconnects for total reconnects.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: RobustWebSocket is not a constructor
Instantiated without 'new' or wrong import (e.g., destructured).
fix
const RobustWebSocket = require('robust-websocket'); const ws = new RobustWebSocket(url);
Module not found: Can't resolve 'robust-websocket'
Package not installed or import path is wrong.
fix
Run 'npm install robust-websocket' and ensure import is 'robust-websocket' (not 'robust-websocket/index').
Cannot read property 'code' of undefined
In shouldReconnect, an online event is received but code is accessed as if it were a CloseEvent.
fix
if (event.type === 'online') { return ... } else { return event.code ... }
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
16
Amazon
1
OpenAI (training)
1
Resources
robust-websocket — npm install robust-websocket · libregistry