Registry / devops / js-rpc2

js-rpc2

JSON →
library2.6.1jsnpmunverified

A lightweight RPC library for JavaScript/TypeScript enabling client-server communication over HTTP and WebSocket. Supports strings, binary data, objects, arrays, and callbacks for progress feedback. Current stable version is 2.6.1. Integrates with Koa router for server-side setup. Ships TypeScript definitions. Differentiated by simple API and broad data type support, including Uint8Array.

npm install js-rpc2
INSTALL
IMPORT
SIG · JS-RPC2
J
js-rpc2
devopsjavascriptv2.6.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.

createRpcClientHttp
import { createRpcClientHttp } from 'js-rpc2/src/client.js'
import { createRpcClientHttp } from 'js-rpc2'
The package exports from src/client.js directly; main entry may not include this symbol.
createRpcClientWebSocket
import { createRpcClientWebSocket } from 'js-rpc2/src/client.js'
import { createRpcClientWebSocket } from 'js-rpc2'
WebSocket client import requires specific path.
createRpcServerKoaRouter
import { createRpcServerKoaRouter } from 'js-rpc2/src/server.js'
import { createRpcServerKoaRouter } from 'js-rpc2'
Server-side Koa router import requires specific path.

Demonstrates setting up a server with Koa and RPC router, and making an RPC call from a client with progress callbacks.

import { createServer } from 'http'; import Koa from 'koa'; import Router from 'koa-router'; import { createRpcServerKoaRouter } from 'js-rpc2/src/server.js'; import { createRpcClientHttp } from 'js-rpc2/src/client.js'; // Server const app = new Koa(); const router = new Router(); app.use(router.routes()); app.use(router.allowedMethods()); createServer(app.callback()).listen(9000); class RpcApi { async hello(string, buffer, object, _null, _undefined, array, callback) { for (let i = 0; i < 3; i++) { callback('progress : ', i); await new Promise((resolve) => setTimeout(resolve, 1000)); } return [ `${typeof string}-${typeof buffer}-${typeof object}-${typeof _null}-${typeof _undefined}-${typeof array}`, new Uint8Array(3), { a: 1, b: 2, c: 3 }, 1, true, undefined, [1, 2, 3, 4], ]; } } const extension = new RpcApi(); createRpcServerKoaRouter({ path: '/rpc/abc', router: router, extension: extension }); // Client const rpc = createRpcClientHttp({ url: 'http://127.0.0.1:9000/rpc/abc' }); let ret = await rpc.hello('123', new Uint8Array(3), { q: 2, w: 3, e: 4 }, null, undefined, [1, 2, 3, 4], (message, num) => { console.info('callback', message, num); }); console.info(ret);
Debug
Known issues
gotchaImports must use specific paths (e.g., 'js-rpc2/src/client.js') instead of the package name. Default main entry may not export these symbols.
fix
Use correct paths: 'js-rpc2/src/client.js' for client functions, 'js-rpc2/src/server.js' for server functions.
affects: >=2.0
breakingVersion 2.0 moved client/server functions to src/ subdirectory, breaking direct imports from 'js-rpc2'.
fix
Update imports to include the full path to src/client.js or src/server.js.
affects: >=2.0
deprecatedThe package does not provide a main export; relying on default import from 'js-rpc2' will return an empty object.
fix
Use named imports from specific subpaths as shown in documentation.
affects: >=2.0
gotchaCallback functions must be the last argument; otherwise, errors may occur during deserialization.
fix
Ensure callback parameters are always the last in the method signature on the server side.
affects: >=2.0
Errors
Common errors & fixes
TypeError: (0 , client.createRpcClientHttp) is not a function
Importing from incorrect path.
fix
Change import to: import { createRpcClientHttp } from 'js-rpc2/src/client.js';
Cannot find module 'js-rpc2' or its corresponding type declarations.
TypeScript cannot resolve module because main entry doesn't point to output.
fix
Use import with explicit path: import { ... } from 'js-rpc2/src/client.js';
Error: callback is not a function
Callback parameter not provided or in wrong position.
fix
Ensure callback is the last argument and is a function.
Upgrade
Version history
2.6.1latest on npm
Audit
Dependencies
wsrequiredRequired for WebSocket server functionality
koa-routerrequiredRequired for Koa router integration on server side
koarequiredRequired for Koa application setup
Agent activity
9 hits · last 30 days
node
8
Resources
js-rpc2 — npm install js-rpc2 · libregistry