Registry / messaging / vietanh-websocket

vietanh-websocket

JSON →
library0.4.3jsnpmunverified

A JavaScript SDK for WebSocket communication with automatic reconnection, heartbeat monitoring, and subscription support. Current version 0.4.3. The package provides a simple client that handles connection lifecycle, reconnection with configurable attempts and delays, and heartbeat tracking to detect disconnections. It emits events for 'connect', 'disconnect', 'message', and 'close'. Key differentiator: built-in heartbeat and reconnection logic out of the box. Suitable for real-time applications requiring persistent WebSocket connections.

npm install vietanh-websocket
INSTALL
IMPORT
SIG · VIETANH-WEBSOCKET
V
vietanh-websocket
messagingjavascriptv0.4.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.

socketClient
import { socketClient } from 'vietanh-websocket'
const socketClient = require('vietanh-websocket')
Package is ESM-only; CommonJS require will fail.
SocketClient
import { SocketClient } from 'vietanh-websocket'
import SocketClient from 'vietanh-websocket'
SocketClient is a named export, not default.
ClientOptions
import type { ClientOptions } from 'vietanh-websocket'
import { ClientOptions } from 'vietanh-websocket'
ClientOptions is a TypeScript interface; use type import for type-only usage.

Shows how to import and use socketClient to connect, subscribe, and handle messages.

import { socketClient } from 'vietanh-websocket'; const client = socketClient({ url: process.env.WS_URL ?? 'wss://example.com/ws', wsKey: process.env.WS_KEY ?? 'default-key', }); client.connect(); client.emitSub('test'); client.on('message', (e) => { console.log('data', e); }); setTimeout(() => client.close(), 5000);
Debug
Known issues
deprecatedThe onMessageCallback option is deprecated in favor of the 'message' event listener.
fix
Use client.on('message', callback) instead of onMessageCallback option.
affects: >=0.4.0
gotchaCalling connect() multiple times without disconnect creates duplicate connections.
fix
Check client.isConnected() before calling connect(), or call close() first.
affects: >=0.4.0
breakingFrom v0.4.0, only ESM imports are supported; CommonJS require() throws an error.
fix
Use import syntax or set type:"module" in package.json.
affects: >=0.4.0
gotchaThe WebSocket URL must use ws:// or wss:// protocol; using http:// will fail silently.
fix
Ensure the URL starts with 'ws://' or 'wss://'.
affects: >=0.4.0
gotchaThe wsKey is required but never validated; a wrong key will not cause an error on connect.
fix
Validate wsKey on the server side; client will connect regardless.
affects: >=0.4.0
Errors
Common errors & fixes
TypeError: socketClient is not a function
Using CommonJS require with an ESM-only package.
fix
Change to ES import: import { socketClient } from 'vietanh-websocket'
Error: 'socketClient' is not exported from 'vietanh-websocket'
Misspelled import name (e.g., socketclient instead of socketClient).
fix
Use correct casing: import { socketClient } from 'vietanh-websocket'
SyntaxError: Cannot use import statement outside a module
Package used in a CommonJS project without type:module.
fix
Add "type": "module" in package.json or rename file to .mjs
Upgrade
Version history
0.4.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

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