Skip to content
/ wsrpc Public

Client and server pieces done in AMD for making JSON-RPC calls via websockets

License

Notifications You must be signed in to change notification settings

monteslu/wsrpc

Repository files navigation

rawr (a.k.a. RAWRpc)

NPM

CircleCI

Coverage Status

Remote Procedure Calls (JSON-RPC) sent over any EventEmitter-based transport. WebWorkers, WebSockets, MQTT, and more!

RAWRpc

Installation

npm install rawr

Using rawr with a webworker

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
}

Using rawr with a websocket

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();
  
});

Handling Notifications

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
});

About

Client and server pieces done in AMD for making JSON-RPC calls via websockets

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •