rawr
JSON-RPC over simple event emitters
Last updated a year ago by monteslu .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install rawr 
SYNC missed versions from official npm registry.

rawr (a.k.a. RAWRpc)

NPM CircleCI

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 remote method calls in either direction.

For example, we can use methods that belong to a webworker.

In our worker.js file:

import rawr, { transports } from 'rawr';

// In this instantiation, we can pass in an object to 
// `methods` that is exposed to our web page (see below)
const peer = rawr({
  transport: transports.worker(),
  methods: { calculatePrimes },
});

function calculatePrimes(howMany) {
  // Do something CPU intensive in this thread that
  // would otherwise be too expensive for our web page
  ...
  return primes;
}

In our web page:

import rawr, { transports } from 'rawr';

const myWorker = new Worker('/worker.js');
const peer = rawr({transport: transports.worker(myWorker)});

// Remote methods are *~automatically available~*
const result = await peer.methods.calculatePrimes(349582);

The methods are available to the rawr peer through the magic of Proxies

Magic

Using rawr with a websocket

We could use rawr to make calls to a remote server such as a websocket. Simply use a different transport.

on our web page:

import rawr, { transports } from 'rawr';

const socket = new WebSocket('ws://localhost:8080');

socket.onopen = (event) => {
  // create the rawr peer
  const peer = rawr({
    transport: transports.websocket(socket)
  });
};

The websocket server could even make arbitrary calls to the client!

on the server:

socketServer.on('connection', (socket) => {
  const peer = rawr({ 
    transport: transports.websocket(socket) 
  });

  const result = 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
});

Current Tags

  • 0.13.0                                ...           latest (a year ago)

18 Versions

  • 0.13.0                                ...           a year ago
  • 0.12.2                                ...           2 years ago
  • 0.12.1                                ...           2 years ago
  • 0.12.0                                ...           2 years ago
  • 0.11.0                                ...           2 years ago
  • 0.10.0                                ...           2 years ago
  • 0.9.1                                ...           2 years ago
  • 0.9.0                                ...           2 years ago
  • 0.8.0                                ...           2 years ago
  • 0.7.0                                ...           3 years ago
  • 0.6.0                                ...           3 years ago
  • 0.5.0                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.2                                ...           5 years ago
  • 0.2.1                                ...           7 years ago
  • 0.2.0                                ...           7 years ago
  • 0.1.0                                ...           8 years ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 4
Dependencies (0)
None
Dev Dependencies (10)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |