Every rawr peer 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
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!