RPC event based router for JavaScript bi-directional messaging
npm install nmsg-rpcnmsg-rpc?
.on('event', cb) and .emit('event', data, cb)
nmsg-rpc comes in.
.emit('event', cb1, 'data', cb2, cb3)
rpc.Api class where you define your API only
.on() to every new connection.
SockJS and socket.io
nmsg.rpc in your browser.
ts
interface ISocket {
onmessage: (msg: any) => void;
send(msg: any);
}
`
(P.S. Websocket is implemented like that.)
Usage:
`ts
import * as rpc from 'nmsg-rpc'; // var rpc = require('nmsg-rpc');
var socket: ISocket; // Socket that can send messages and receive messages.
var router = new rpc.Router;
// Proxy the messages to your router.
router.send = (obj) => { socket.send(obj); };
socket.onmessage = (obj) => { router.onmessage(obj); };
`
You do this for both of your sockets: the server one and the client one. Now,
you can use your newly created router like so:
`js
// On server
router.on('ping', function(callback) {
callback('pong');
});
// On client
router.emit('ping', function(result) {
console.log(result); // pong
});
`
You can use wildcard "*" event to capture all imcoming messages:
`ts
router.on('*', function(event, ...args: any[]) {
// All incoming messages here.
// If any callbacks in args list, make sure you call it only once
// as this message will be passed to corresponding event callback as well.
});
`
Reference
$3
`ts
class Router {
send: (data) => void;
onmessage(msg: any): void;
onerror: (err) => void;
onevent: (event: string, args: any[]) => void;
setApi(api: Api): this;
on(event: string, callback: TeventCallback): this;
emit(event: string, ...args: any[]): this;
}
`
- .send(data) -- you have to implement this function.
- .onmessage(msg) -- you have to call this function when new messages arrive.
- .on() and .emit() -- use these two methods to do all your communication between the processes.
- .onerror(err) -- you can implement this function to listen for parsing errors.
- .onevent(event: string, args: any[]) -- implement this function to wiretap on all incoming events.
$3
rpc.RouterBuffered is almost the same as rpc.Router except it buffers all outgoing .emit() calls
for 10 milliseconds and then combines them into one bulk request and flushes it, thus combining many small
calls into one bigger request.
$3
On server side you actually don't want to add .on() event callbacks for every
socket. Imagine you had 1,000 .on() callbacks for each socket and 1,000
live sockets, you would need to create 1,000,000 functions for that.
To avoid that, server-side use rpc.Api class to define all your functions
only once like so:
`ts
var api = new rpc.Api()
.add({
method1: function() { /.../ },
method2: function() { /.../ },
})
.add({
method3: function() { /.../ },
});
var router = new rpc.Router;
router.setApi(api);
`
Methods defined in rpc.Api will "overwrite" equally named events attached using .on().
TypeScript type definitions available in ./nmsg-rpc.d.ts.
Examples
Originally nmsg-rpc was part of the nmsg project but it is so useful
on its own that we have carved it out into a standalone package. At only 344 lines of code (as of this writing)
and no external dependencies, nmsg-rpc is lightweight enough for you to use in almost any project.
Below we will take at the following use cases:
- Talking with a web Worker
- Talking with