Registry / messaging / json-rpc-ws

json-rpc-ws

JSON →
library7.0.1jsnpmunverified

A WebSocket transport library for JSON-RPC that enables asynchronous bidirectional requests and responses over WebSocket connections. Version 7.0.1 supports Node.js and browser environments via separate entry points. It provides a simple API with server and client creation, method exposure, and callback-based request/response handling. Key differentiators: uses the popular 'ws' library for Node, supports both server and client in one package, and offers a lightweight abstraction over raw WebSocket messaging.

npm install json-rpc-ws
INSTALL
IMPORT
SIG · JSON-RPC-WS
J
json-rpc-ws
messagingjavascriptv7.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.

default (the module itself)
import * as JsonRpcWs from 'json-rpc-ws'
const JsonRpcWs = require('json-rpc-ws')
In Node.js, CJS require() works but ESM import * is recommended. For browser, use 'json-rpc-ws/browser'.
server instance
const server = JsonRpcWs.createServer()
const server = new JsonRpcWs.Server()
createServer() is the factory, not a constructor.
client instance
const client = JsonRpcWs.createClient()
const client = new JsonRpcWs.Client()
createClient() is the factory, not a constructor.

Shows server creation, method exposure, and callbacks for request/response over WebSocket.

const JsonRpcWs = require('json-rpc-ws'); // Create server const server = JsonRpcWs.createServer(); // Expose a method server.expose('hello', function (params, reply) { reply(null, { greeting: 'Hello, ' + params[0] }); }); // Start server server.start({ port: 8080 }, function () { console.log('Server started'); }); // In a separate client script: // const client = JsonRpcWs.createClient(); // client.connect('ws://localhost:8080', function () { // client.send('hello', ['World'], function (err, reply) { // console.log(reply); // { greeting: 'Hello, World' } // }); // });
Debug
Known issues
gotchaws library versions >=1.0.0 removed client code for browsers; use the browser entry point 'json-rpc-ws/browser'.
fix
import { ... } from 'json-rpc-ws/browser' in browser environments.
affects: >=1.0.0
deprecatedThe 'ws' library is a peer dependency; ensure compatibility with your project's ws version.
fix
Install 'ws' alongside: npm install ws json-rpc-ws
affects: all
gotchaMethod handlers can only have one reply per request; sending multiple replies or non-null both error and reply will cause issues.
fix
Always call reply exactly once with either error or reply as non-null, not both.
affects: all
Errors
Common errors & fixes
Cannot find module 'ws'
Missing peer dependency ws.
fix
npm install ws
Uncaught ReferenceError: WebSocket is not defined
Using Node-appropriate import in browser (e.g., 'json-rpc-ws' instead of 'json-rpc-ws/browser').
fix
Use 'json-rpc-ws/browser' in browser code.
handler already exposed: methodName
Attempting to expose a method that already has a handler.
fix
Use a different method name or remove the existing handler first.
Upgrade
Version history
7.0.1latest on npm
Audit
Dependencies
wsrequiredCore WebSocket library used for Node.js server and client. Browser builds use the native WebSocket API.
Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
json-rpc-ws — npm install json-rpc-ws · libregistry