@primafuture/remote-async-call
v2.0.3
Published
RPC + Pub/Sub over WebSockets
Downloads
11
Readme
@primafuture/remote-async-call
Installation
npm install @primafuture/remote-async-call
Package can be used directly in the browser without the use of package managers/bundlers as well: UMD version from unpkg.com.
Usage
import rac from '@primafuture/remote-async-call';
// create connection
const ws = new websocket.WebSocket("ws://...");
const racProxy = new rac.RACProxy(new rac.WebsocketCommunicationConnection(ws));
// wait for connection
racProxy.onConnected(() => {
// ...
});
RPC model
// register method
racProxy.register('sum', ([a, b]:[number, number]) => {
const res = a + b;
return res;
});
// call method
let result = await racProxy.call('sum', [2, 3]);
console.log(result);
Pub/Sub model
// subscribe event
racProxy.subscribe('event', (args) => {
console.log('on event', args);
});
// publish event
await racProxy.publish('event', [1, 2, 3]);