Registry / messaging / json-rpc2

json-rpc2

JSON →
library2.0.0jsnpmunverified

JSON-RPC 2.0 server and client library supporting HTTP, TCP, and WebSocket transports for Node.js. Version 2.0.0 (latest) uses native EventEmitter and enforces RFC-compliant request id types (String, Number, or null). Released under MIT license with moderate maintenance cadence. Differentiators include simple API, built-in authentication for TCP sockets, class extension via ES5Class mixins, and WebSocket support out of the box. Requires Node >= 10.

npm install json-rpc2
INSTALL
IMPORT
SIG · JSON-RPC2
J
json-rpc2
messagingjavascriptv2.0.0
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.

Server
import rpc from 'json-rpc2'; const server = rpc.Server.$create(...)
import { Server } from 'json-rpc2'
Package exports a single default object; named imports will be undefined.
Client
const rpc = require('json-rpc2'); const client = rpc.Client.$create(8000, 'localhost');
const { Client } = require('json-rpc2')
CommonJS require returns the default export; destructuring Client directly will yield undefined.
Endpoint
import rpc from 'json-rpc2'; rpc.Endpoint.$include({ ... });
import { Endpoint } from 'json-rpc2'
Use default import then access Endpoint property.

Creates an HTTP+WebSocket JSON-RPC 2.0 server with an 'add' method and a client that calls it.

import rpc from 'json-rpc2'; const server = rpc.Server.$create({ websocket: true, headers: { 'Access-Control-Allow-Origin': '*' } }); function add(args, opt, callback) { callback(null, args[0] + args[1]); } server.expose('add', add); server.listen(8000, 'localhost', () => { console.log('JSON-RPC 2.0 server listening on port 8000'); }); const client = rpc.Client.$create(8000, 'localhost'); client.call('add', [1, 2], (err, result) => { if (err) console.error(err); else console.log('1 + 2 =', result); });
Debug
Known issues
breakingid member now enforces RFC compliance: only String, Number, or null allowed. Non-conforming ids (e.g. objects, arrays) will be rejected.
fix
Ensure all request ids are String, Number, or null.
affects: >=1.0.0
breakingEventEmitter3 replaced with native EventEmitter in version 2.0.0. Code relying on EventEmitter3-specific behavior (e.g., wildcard events) may break.
fix
Do not depend on EventEmitter3-specific APIs; use standard EventEmitter patterns.
affects: >=2.0.0
deprecatedThe package is not actively maintained; last update was in 2019. No security patches for Node.js 14+ vulnerabilities.
fix
Consider migrating to alternatives like 'jayson' or 'json-rpc-2.0'.
affects: *
gotchaDefault export is an object; named imports (like `import { Server } from 'json-rpc2'`) will be undefined.
fix
Use default import: `import rpc from 'json-rpc2'` then access `rpc.Server`.
affects: *
gotcha`Client.$create` expects host as second argument; passing a string with protocol (e.g., 'http://localhost') will fail silently.
fix
Pass only hostname and port separately: `Client.$create(8000, 'localhost')`.
affects: *
Errors
Common errors & fixes
TypeError: rpc.Server is undefined
Using named import instead of default import.
fix
Change to `import rpc from 'json-rpc2'` then use `rpc.Server`.
Error: Invalid id
Request id is not a String, Number, or null.
fix
Ensure id is a string, number, or null as per JSON-RPC 2.0 spec.
TypeError: server.listenRaw is not a function
Using `listenRaw` on an HTTP server; `listenRaw` is only for raw TCP servers created without HTTP options.
fix
Create a raw server with `rpc.Server.$create()` and then call `server.listenRaw(8080, 'localhost')`.
TypeError: Cannot read properties of undefined (reading '$create')
Forgot to import json-rpc2 or imported incorrectly.
fix
Use `const rpc = require('json-rpc2')` or `import rpc from 'json-rpc2'`.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
es5classrequiredRuntime dependency for class inheritance and mixins
Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
json-rpc2 — npm install json-rpc2 · libregistry