Registry / messaging / sockette-dynamic-url

sockette-dynamic-url

JSON →
library2.0.7jsnpmunverified

Sockette is a tiny (367 bytes) WebSocket wrapper that automatically reconnects on connection loss. Current stable version is 2.0.7. Released on npm, it provides a minimal API for managing WebSocket connections with configurable timeout and max reconnection attempts. It supports event listeners for open, message, close, reconnect, and maximum attempts reached events. Unlike raw WebSocket, it allows reusing a single instance without redeclaring listeners. It ships TypeScript types and is compatible with both browser and Node.js (with WebSocket polyfill).

npm install sockette-dynamic-url
INSTALL
IMPORT
SIG · SOCKETTE-DYNAMIC-U
S
sockette-dynamic-url
messagingjavascriptv2.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.

Sockette
import Sockette from 'sockette'
const Sockette = require('sockette')
Sockette is a default export. ESM import is recommended but CJS require works.
Sockette
import { Sockette } from 'sockette'
Named import also works due to named export in package. Both default and named are supported.
Sockette
import Sockette from 'sockette'
import { default as Sockette } from 'sockette'
Redundant but not incorrect. Prefer direct default import.

Shows creating a Sockette instance with all event handlers, sending text and JSON, and manually reconnecting after 10 seconds.

import Sockette from 'sockette'; const ws = new Sockette('wss://example.com/ws', { timeout: 5000, maxAttempts: 10, onopen: e => console.log('Connected'), onmessage: e => console.log('Received:', e.data), onreconnect: e => console.log('Reconnecting...'), onmaximum: e => console.log('Max attempts reached'), onclose: e => console.log('Closed'), onerror: e => console.log('Error:', e) }); ws.send('Hello'); ws.json({ type: 'ping' }); setTimeout(() => ws.reconnect(), 10000);
Debug
Known issues
gotchaPassing `maxAttempts: -1` disables reconnection attempts, but this is counterintuitive. Use Infinity or omit to keep default.
fix
Set `maxAttempts: Infinity` or omit the option.
affects: >=0.0.0
gotchaThe `timeout` option is in milliseconds, not seconds. Common mistake to pass 5 instead of 5000.
fix
Ensure timeout value is in milliseconds (e.g., 5000 for 5 seconds).
affects: >=0.0.0
gotchaSockette does not polyfill WebSocket for Node.js. Must provide a WebSocket implementation (e.g., `ws`) in non-browser environments.
fix
Use a WebSocket polyfill like `ws` and assign globally: `global.WebSocket = require('ws');`
affects: >=0.0.0
gotchaEvent listeners passed in options are bound to the Sockette instance, not the WebSocket. Access the underlying WebSocket via `event.target`.
fix
Use `event.target` to access the WebSocket instance inside event handlers.
affects: >=0.0.0
gotchaSockette resets its attempt counter after a successful connection, so reconnection behavior may not be as expected after a full reconnect cycle.
fix
Be aware that the counter resets; plan logic accordingly if you need persistent retry tracking.
affects: >=0.0.0
Errors
Common errors & fixes
Error: WebSocket is not defined
Using Sockette in Node.js without a WebSocket polyfill.
fix
Install `ws` and assign `global.WebSocket = require('ws');` before using Sockette.
TypeError: ws.send is not a function
Calling send on the Sockette instance but the connection is not yet open.
fix
Ensure send is called after the `onopen` event has fired, or inside a callback.
Uncaught DOMException: Failed to construct 'WebSocket': The URL 'ws://example.com' is invalid.
URL string missing protocol (ws:// or wss://) or malformed.
fix
Use a valid WebSocket URL with proper scheme, e.g., 'wss://example.com/ws'.
TypeError: sockette_1.default is not a constructor
Using named import `{ Sockette }` when only default export exists (or vice versa).
fix
Use `import Sockette from 'sockette'` (default import) or `const Sockette = require('sockette')`.
Upgrade
Version history
2.0.7latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
sockette-dynamic-url — npm install sockette-dynamic-url · libregistry