Fast JSON RPC 2.0 written in TypeScript.
TypeScript JSON RPC 2.0 WebSocket implementation with async-await Promises.
Description
I really lacked typescript support or type definitions of rpc-websockets. I kept everything as simple as possible for best performance and in principle stay close to the metal. Under the hood id-generation for requests is done using uuid/v1 to provide id uniqueness as an additional feature.
Installation
npm i rpc-websocket-client
Features
- TypeScript with documentation in comments.
- Unique RPC identifiers by uuid/v1.
- Lightweight. Allows you to call
noRpc()
method to prevent sending jsonrpc: '2.0'
overhead from all messages if you'd like to ignore the JSON RPC 2.0 standard for better performance. - Option to connect RpcWebSocketClient with already existing WebSocket with
changeSocket()
and listenMessages()
methods. Useful if you use REST or GraphQL implementation from another library and want to handle JSON RPC 2.0 when communicating from server to client (that was my use case to develop this package).
Basic Usage
import { RpcWebSocketClient } from 'rpc-websocket-client';
(async () => {
const rpc = new RpcWebSocketClient();
await rpc.connect(`ws://localhost:4000/`);
await rpc.call(`auth.login`, [`rpcMaster`, `mySecretPassword`]);
await rpc.call(`auth.login`, [`rpcMaster`, `mySecretPassword`]).then(() => {
}).catch((err) => {
await rpc.call(`auth.signup`, {
login: `rpcMaster`,
password: `mySecretPassword`,
});
});
rpc.notify(`btw.iHateYou`, [`over and out`]);
})();
Advanced Usage
import { RpcWebSocketClient } from 'rpc-websocket-client';
(async () => {
const ws = (apollo as any).client.wsImpl;
const rpc = new RpcWebSocketClient();
rpc.onRequest.push((data) => {
});
rpc.onNotification.push((data) => {
});
rpc.changeSocket(ws);
rpc.listenMessages();
})();