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);
Errors
Common errors & fixes
TypeError: (0 , client.createRpcClientHttp) is not a function
Importing from incorrect path.
fixChange 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.
fixUse 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.
fixEnsure callback is the last argument and is a function.
Audit
Dependencies
wsrequiredRequired for WebSocket server functionality
koa-routerrequiredRequired for Koa router integration on server side
koarequiredRequired for Koa application setup