Registry / messaging / websocket-as-promised

websocket-as-promised

JSON →
library3.0.1jsnpmunverified

A WebSocket client library providing a Promise-based API for connecting, disconnecting, and messaging with servers. Version 3.0.1 is the latest stable release. It supports both browser and Node.js environments with ESM and CommonJS modules. Key differentiators include built-in TypeScript types, support for raw, JSON, and binary data, and a request/response pattern. The library is actively maintained and requires a global Promise constructor or polyfill for older environments.

npm install websocket-as-promised
INSTALL
IMPORT
SIG · WEBSOCKET-AS-PROMI
W
websocket-as-promised
messagingjavascriptv3.0.1
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.

WebSocketAsPromised
import WebSocketAsPromised from 'websocket-as-promised'
const WebSocketAsPromised = require('websocket-as-promised')
Default export; CommonJS require works in Node.js, but TypeScript types are better with ESM import.
WebSocketAsPromised
import WebSocketAsPromised from 'websocket-as-promised'
import { WebSocketAsPromised } from 'websocket-as-promised'
The library exports a single default class, not a named export. Named import will result in undefined.
WebSocketAsPromised type
import WebSocketAsPromised from 'websocket-as-promised'
import type WebSocketAsPromised from 'websocket-as-promised'
TypeScript type imports should use the default import syntax; the library provides type declarations alongside the class.

Shows how to create a WebSocket client with JSON packing, send a request and await response, then close connection.

import WebSocketAsPromised from 'websocket-as-promised'; const wsp = new WebSocketAsPromised('ws://example.com', { packMessage: data => JSON.stringify(data), unpackMessage: data => JSON.parse(data), extractMessageData: event => event.data, // for browser }); (async () => { try { await wsp.open(); const response = await wsp.sendRequest({ type: 'ping' }); console.log('Response:', response); } catch (err) { console.error('Error:', err); } finally { await wsp.close(); } })();
Debug
Known issues
gotchaWhen using the 'ws' package in Node.js, omitting extractMessageData option causes data to be undefined.
fix
Set extractMessageData: event => event in constructor options.
affects: >=0.0.0
gotchasendRequest() returns a promise that may never resolve if no matching response is received; ensure server sends a response with matching requestId.
fix
Define packMessage and unpackMessage options for automatic request/response tracking, or implement timeout logic.
affects: >=0.0.0
gotchaThe library expects a global Promise constructor; in environments without native Promise, install a polyfill.
fix
Install 'es6-promise' or similar polyfill, or use Node.js >=10 which includes native Promise.
affects: >=0.0.0
breakingVersion 3.0 removed the `onOpen`, `onClose`, `onError` event properties; use `onStatusChange` or `open()`, `close()` promises instead.
fix
Migrate from `wsp.onOpen.addListener(...)` to `wsp.onStatusChange.addListener(...)` or handle via `await wsp.open()`.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'data')
In Node.js with 'ws', the onmessage handler receives the WebSocket frame directly, not a MessageEvent.
fix
Add extractMessageData: event => event to constructor options.
Error: open() called when connection already open
Calling open() on an already opened WebSocket.
fix
Check wsp.isOpening or handle connection state before calling open().
Error: Request timeout
sendRequest() waiting for response but server never sent one with matching requestId.
fix
Ensure server sends a response with the same ID, or implement custom timeout logic using Promise.race.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies
wsoptionalOptional WebSocket implementation for Node.js; required when using the 'ws' package instead of browser WebSocket.
websocketoptionalOptional WebSocket implementation for Node.js; provides W3C-compatible WebSocket client.
Agent activity
23 hits · last 30 days
node
20
OpenAI (training)
2
Resources
websocket-as-promised — npm install websocket-as-promised · libregistry