Registry / messaging / observable-websocket

observable-websocket

JSON →
library0.1.3jsnpmunverified

A WebSocket client that enables interaction via RxJS-like subscriptions. Version 0.1.3 is the current stable release. It automatically manages connection lifecycle: establishes connection on first subscribe, reconnects up to a configurable limit on unexpected disconnection, and closes when the last consumer unsubscribes. Differentiating features include observable-based message handling and built-in reconnection logic. Suitable for Node.js >=8.0.0.

npm install observable-websocket
INSTALL
IMPORT
SIG · OBSERVABLE-WEBSOCK
O
observable-websocket
messagingjavascriptv0.1.3
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.

ObservableWebsocket
import { ObservableWebsocket } from 'observable-websocket'
const ObservableWebsocket = require('observable-websocket')
Package ships CommonJS; named export via destructuring.
ObservableWebsocket (type)
import type { ObservableWebsocket } from 'observable-websocket'
import { ObservableWebsocket } from 'observable-websocket'
For type-only imports in TypeScript, use import type to avoid runtime side effects.
ObservableWebsocket (constructor)
const ws = new ObservableWebsocket({ uri: 'wss://example.com' })
const ws = new ObservableWebsocket('wss://example.com')
Options object with `uri` property is required, not string.
subscribe/unsubscribe
const consumer = (msg) => console.log(msg); await ws.subscribe({ consumer });
ws.subscribe(consumer)
subscribe expects an object with consumer property, not just the function.
send
await ws.send({ data: 'hello' })
ws.send('hello')
send expects an object with a data property.

Creates an ObservableWebSocket, subscribes with a consumer that logs incoming messages, sends a message, and after 2 seconds unsubscribes.

import { ObservableWebsocket } from 'observable-websocket'; const ws = new ObservableWebsocket({ uri: 'wss://echo.websocket.org' }); const consumer = ({ event }) => console.log('Received:', event.data); async function main() { await ws.subscribe({ consumer }); await ws.send({ data: 'Hello WebSocket!' }); // Keep connection open for a moment to receive echo setTimeout(async () => { await ws.unsubscribe({ consumer }); console.log('Done.'); }, 2000); } main().catch(console.error);
Debug
Known issues
gotchasubscribe/unsubscribe expect an object with a `consumer` property, not just the callback function.
fix
Use `{ consumer }` wrapper as shown in documentation.
affects: >=0.0.0
gotchasend expects an object with a `data` property, not plain string or Buffer.
fix
Use `{ data: payload }` format.
affects: >=0.0.0
deprecatedmaxConnectionAttempts: 0 means unlimited retries; this may be a source of infinite loops.
fix
Set to a finite number (e.g., 5) to limit reconnection attempts.
affects: >=0.0.0
gotchaCalling subscribe multiple times with the same consumer will register it multiple times, causing duplicate message handling.
fix
Ensure consumer uniqueness or track subscription state externally.
affects: >=0.0.0
breakingObservableWebsocket does not expose the underlying WebSocket instance; raw socket manipulation is not possible.
fix
Use only the provided methods (subscribe, unsubscribe, send) for interaction.
affects: >=0.0.0
gotchaDebug option logs all events to console; may leak sensitive data in production.
fix
Set `debug: false` in production.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: ObservableWebsocket is not a constructor
Importing default export instead of named.
fix
Use `import { ObservableWebsocket } from 'observable-websocket'` (curly braces).
Error: Invalid URI: expected string, got undefined
Creating ObservableWebsocket without options or without `uri` property.
fix
Pass an object with `uri` property: `new ObservableWebsocket({ uri: 'wss://...' })`.
TypeError: send is not a function
Calling `send` on an instance before proper import.
fix
Ensure named destructured import: `import { ObservableWebsocket } from 'observable-websocket'`.
Error: No consumer provided. Cannot subscribe without consumer
Calling subscribe with a function instead of object.
fix
Wrap consumer in object: `ws.subscribe({ consumer: myFunc })`.
Upgrade
Version history
0.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources