Registry / messaging / shopify-reconnecting-websocket

shopify-reconnecting-websocket

JSON →
library0.1.0jsnpmunverified

A lightweight (<600 bytes gzipped) drop-in replacement for the native WebSocket API that automatically reconnects when the connection drops. Version 0.1.0 is a fork of the original reconnecting-websocket library, updated to track the latest master branch. It is API-compatible with WebSocket, so you can replace 'new WebSocket(url)' with 'new ReconnectingWebSocket(url)' and get automatic reconnection with exponential backoff (configurable via options like reconnectInterval, maxReconnectInterval, reconnectDecay). It supports all standard WebSocket events (onopen, onmessage, onclose, onerror) and methods (send, close). Compared to alternatives, it is minimal and has no dependencies, but note that this fork may not see active maintenance.

npm install shopify-reconnecting-websocket
INSTALL
IMPORT
SIG · SHOPIFY-RECONNECTI
S
shopify-reconnecting-websocket
messagingjavascriptv0.1.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.

ReconnectingWebSocket
import ReconnectingWebSocket from 'shopify-reconnecting-websocket'
const ReconnectingWebSocket = require('shopify-reconnecting-websocket')
This package does not export a named export; it uses a default export. In CommonJS, use require().default or import syntax.
ReconnectingWebSocket
const { default: ReconnectingWebSocket } = require('shopify-reconnecting-websocket')
const ReconnectingWebSocket = require('shopify-reconnecting-websocket')
CommonJS require() returns an object with a default property. Direct assignment leads to undefined.
ReconnectingWebSocket
var ws = new ReconnectingWebSocket('ws://...');
var ws = new WebSocket('ws://...'); // Does not auto-reconnect
ReconnectingWebSocket is a drop-in replacement for WebSocket. Just change the constructor name.

Shows basic usage: import, instantiate with options, and event listeners for open, message, close, and error events.

import ReconnectingWebSocket from 'shopify-reconnecting-websocket'; const socket = new ReconnectingWebSocket('wss://example.com/ws', null, { debug: true, reconnectInterval: 1000, maxReconnectInterval: 30000, reconnectDecay: 1.5, timeoutInterval: 2000, maxReconnectAttempts: null }); socket.addEventListener('open', () => { console.log('Connected'); socket.send('Hello server!'); }); socket.addEventListener('message', (event) => { console.log('Message from server:', event.data); }); socket.addEventListener('close', () => { console.log('Disconnected'); }); socket.addEventListener('error', (err) => { console.error('WebSocket error:', err); }); // To manually close: // socket.close(1000, 'Goodbye');
Debug
Known issues
gotchaThis package does not export a named export, only a default export. Using CommonJS require() directly returns an object with a default property.
fix
Use import ReconnectingWebSocket from 'shopify-reconnecting-websocket' or const { default: ReconnectingWebSocket } = require('shopify-reconnecting-websocket')
affects: >=0.0.0
gotchaThe addEventListener method uses standard event types as strings (e.g., 'open', 'message'), but custom properties like 'onopen' work as direct callbacks. Setting both may cause unexpected behavior.
fix
Use only one style: either addEventListener or onopen, not both simultaneously.
affects: >=0.0.0
deprecatedThis is a fork of the original reconnecting-websocket, which may not receive updates. Consider using the original library or a more modern alternative.
fix
Switch to the original reconnecting-websocket or use robust-ws.
affects: >=0.0.0
gotchaThe `binaryType` option expects 'blob' or 'arraybuffer', but must be set before the connection opens. Setting it after open() may not take effect on some environments.
fix
Set binaryType in the options object or immediately after instantiation and before opening.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: ReconnectingWebSocket is not a constructor
CommonJS require() returns an object with a default export, not the class directly.
fix
Use const { default: ReconnectingWebSocket } = require('shopify-reconnecting-websocket')
index.js:1 Uncaught TypeError: Cannot read property 'addEventListener' of undefined
Attempting to use the module without proper import; the symbol is undefined.
fix
Ensure correct import: import ReconnectingWebSocket from 'shopify-reconnecting-websocket'
Error: WebSocket is not defined
Running in a non-browser environment (e.g., Node.js) without a WebSocket polyfill.
fix
Install and import a WebSocket implementation like 'ws' and assign to global: global.WebSocket = require('ws')
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
shopify-reconnecting-websocket — npm install shopify-reconnecting-websocket · libregistry