Remote Procedure Calls (JSON-RPC) sent over any EventEmitter-based transport. WebWorkers, WebSockets, MQTT, and more!
npm install rawr
Every rawr client can act as both a client and a server, and make RPC calls in either direction.
For example, if we want to use rawr to make calls to a webworker:
import rawr from 'rawr';
import { dom } from 'rawr/tansports/worker';
const myWorker = new Worker('/my-worker.js');
const peer = rawr({transport: dom(myWorker)});
const result = await peer.methods.doSomething('lots of data');
Our WebWorker code might look something like:
import rawr from 'rawr';
import { worker } from 'rawr/tansports/worker';
const peer = rawr({transport: worker(), handlers: {doSomething}});
function doSomething(inputData) {
// do some heavy lifting in this thread
// return a result
}
We could even to this type of call to a remote server such as a websocket. Simply use a differnt transport:
import rawr from 'rawr';
import wsTransport from 'rawr/tansports/websocket';
const ws = new WebSocket('ws://localhost:8080');
ws.onopen = (event) => {
// create the rawr peer
const peer = rawr({transport: wsTransport(ws)});
};
The websocket server could even make arbitrary calls to the client!
socketServer.on('connection', (socket) => {
const peer = rawr({ transport: wsTransport(socket) })
const val = await peer.methods.doSomethingOnClient();
});
Peers can also send each other notifications:
peer.notifiers.saySomething('hello');
Receiving those notifications from another peer is just as simple:
peer.notifications.onsaySomething((words) => {
console.log(words); //hello
});