Registry / messaging / rpc-websocket-client

rpc-websocket-client

JSON →
library1.1.4jsnpmunverified

Fast JSON-RPC 2.0 WebSocket client written in TypeScript. Version 1.1.4 is the latest stable release (no recent updates). It provides async-await Promise-based RPC calls and notifications over WebSocket with automatic unique request IDs generated by uuid/v1. Key differentiators: lightweight, close-to-metal design for performance, optional noRpc() mode to skip JSON-RPC overhead, and ability to attach to an existing WebSocket instance (useful with Apollo GraphQL or REST libraries). Focuses on simplicity and TypeScript support, unlike alternatives like rpc-websockets which lack TypeScript declarations. Suitable for Node.js (>=6) and modern browsers with WebSocket.

npm install rpc-websocket-client
INSTALL
IMPORT
SIG · RPC-WEBSOCKET-CLIE
R
rpc-websocket-client
messagingjavascriptv1.1.4
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.

RpcWebSocketClient
import { RpcWebSocketClient } from 'rpc-websocket-client'
const RpcWebSocketClient = require('rpc-websocket-client').RpcWebSocketClient
Package is ESM-only, but also provides CommonJS via main field. The import shown is the standard TypeScript/ESM usage.
RpcResponse
import type { RpcResponse } from 'rpc-websocket-client'
import { RpcResponse } from 'rpc-websocket-client'
Type imports are recommended for types to avoid runtime overhead. RpcResponse is only a type.
RpcError
import { RpcError } from 'rpc-websocket-client'
import { RpcError } from 'rpc-websocket-client' (if runtime error handling)
RpcError is a runtime class (Error subclass) and can be used in catch clauses. Import it normally.

Shows how to create an RPC client, connect, make a call with error handling, send a notification, and close.

import { RpcWebSocketClient } from 'rpc-websocket-client'; async function main() { const rpc = new RpcWebSocketClient(); await rpc.connect('ws://localhost:4000'); try { const result = await rpc.call('auth.login', ['user', 'pass']); console.log('Login successful:', result); } catch (error) { console.error('Login failed:', error); } rpc.notify('app.heartbeat', [{ timestamp: Date.now() }]); rpc.close(); } main();
Debug
Known issues
gotchaIf you use the 'changeSocket()' method, you must also call 'listenMessages()' after changing the socket to start listening. Failing to do so will cause the client to not receive responses.
fix
Always call rpc.listenMessages() after rpc.changeSocket(newWs).
affects: >=1.0.0
deprecatedThe 'call' method will reject with an RpcError if the server returns an error object. Ensure you handle rejections to avoid unhandled promise rejections.
fix
Use .catch() or try/catch around await rpc.call(...).
affects: >=1.0.0
gotchaWebSocket 'close' event may not be properly handled if you close the WebSocket externally after using 'changeSocket'. The client's internal state may not reflect the closed connection.
fix
Use rpc.close() instead of directly closing the underlying WebSocket.
affects: >=1.0.0
gotchaThe 'notify' method does not return a Promise and does not catch errors. If the WebSocket is not connected, an exception may be thrown synchronously.
fix
Ensure the client is connected before calling notify, or wrap in try/catch.
affects: >=1.0.0
Errors
Common errors & fixes
Error: RpcWebSocketClient is not a constructor
Using wrong import pattern or missing default import.
fix
Use import { RpcWebSocketClient } from 'rpc-websocket-client' instead of import RpcWebSocketClient from 'rpc-websocket-client'.
TypeError: Cannot read properties of undefined (reading 'send')
Attempting to call rpc.call or rpc.notify before the WebSocket connection is established.
fix
Always await rpc.connect() before making calls.
UnhandledPromiseRejectionWarning: RpcError: Method not found
Server returned an error for a method that doesn't exist, but the promise rejection was not caught.
fix
Add a .catch() handler to rpc.call() or use try/catch.
Upgrade
Version history
1.1.4latest on npm
Audit
Dependencies
uuidrequiredRequired for generating unique RPC identifiers using uuid/v1.
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
rpc-websocket-client — npm install rpc-websocket-client · libregistry