Noice Json Rpc
Client and Server helpers to implement a clean function based Api for Json Rpc.
Noice Json Rpc takes in a websocket like object. It calls send(msg:str)
function and expects messages to come from on('message', handler)
. It works out of the box with WebSockets but it can also work with stdin/stdout, worker threads, iframes or any other mechanism in which strings can be sent or received.
Its only dependency is events.EventEmitter
.
Using ES6 proxies it exposes a clean client-server api. Since its written in TypeScript, the api object can be cast to work off an interface specific to the domain. e.g ChromeDevTools/devtools-protocol
Example
{ try const rpcClient = 'ws://localhost:8080' logConsole: true const api: DevToolsProtocol.ProtocolApi = rpcClient await Promiseall apiRuntime apiProfiler await apiRuntime await apiProfilerstart await apiRuntime; // Wait for event const result = await apiProfiler console catch e console }
Output
Client > {"id":1,"method":"Runtime.enable"}
Client > {"id":3,"method":"Profiler.enable"}
Client > {"id":4,"method":"Runtime.run"}
Client < {"id":1,"result":{}}
Client < {"id":2,"result":{}}
Client < {"id":3,"result":{}}
Client < {"id":4,"result":{}}
Client > {"id":5,"method":"Profiler.start"}
Client < {"id":5,"result":{}}
Client < {"method":"Runtime.executionContextDestroyed"}
Client > {"id":6,"method":"Profiler.stop"}
Client < {"id":6,"result":{"data":"noice!"}}