duplex-json-rpc
Transport layer independent JSON-RPC library with calls in both directions (i.e. the server can perform calls on the client).
See the example project for WebSocket usage examples.
Client usage
import * as JsonRpc from 'duplex-json-rpc';
type Context = ...;
const client = new JsonRpc.Client<Context>((packet, context) => {
...
});
const result = await client.call('myMethod', { myParameter: 5 }, myContext);
Server usage
import * as JsonRpc from 'duplex-json-rpc';
type Context = ...;
const callHandler: JsonRpc.CallHandler<Context> = {
async myMethod(params, context): Promise<MyResult> {
return ...;
}
};
const server = new JsonRpc.Server<Context>(callHandler, (packet, context) => {
...
});
...
await server.onRequestPacket(myReceivedMessage.toString(), myContext);
Duplex server usage
import * as JsonRpc from 'duplex-json-rpc';
type Context = ...;
const callHandler: JsonRpc.CallHandler<Context> = {
async myMethod(params, context): Promise<MyResult> {
return ...;
}
};
const server = new JsonRpc.DuplexServer<Context>(callHandler, (packet, context) => {
...
});
...
await server.onDataPacket(myReceivedMessage.toString(), myContext);
await server.call('myMethod', { myParameter: 5 }, myContext);