The easiest RPC you've seen, entirely promise based, that plays well with:
- all environments: Node, Browser, Electron etc
- all transports: TPC, sockets, whatever you fancy?
- all Promise-A implemenations: Bluebird, Q, ES6, jQuery etc
- all node versions: 0.10 +, es6 or not
RPC nodes are the core of the API. The transport is defined by you, so you can do whatever you like. There are built-in transports.
A design goal is to let client code be as uncluttered by RPC stuff as possible (e.g calling a method without worrying about having to connect first), while giving control to handle timeouts etc that're vital with RPC.
All methods return a promise. they will timeout if the remote end never turns up. Documentation below uses TypeScript as it's a nicely defined way of talking about types!
interface RpcOptions {
name: string
Promise: PromiseConstructor
timeout: number
emitTimeout: number
error: (err) => void
wrapEffects: () => void
}
interface RpcTransport {
send: () => void
}
returns a new RpcPair
expose either a single or an object of methods to remote side. methods can be sync or async; always async for remote side
node.expose("answer", function(number) {
return number + 42;
});
Takes call options as optional first argument (useful for .bind
/_.partial
to create
an API with default timeouts etc):
e.g
node.call("increment", 1).then(assertEqual(2));
node.call({ timeout: 5000 }, "increment", 1).then(assertEqual(2));
listen to events emitted by remote side. arguments as per EventEmitter
emit event heard by remote. returns promise resolved if remote side turned up to heard about it
RPCjs also supports the ideas of Actors. You'll frequently be wanting to talk about a given context - actors give you a way to to this without continually resending the same context ID.
Returns an object used to interact with a remote actor.
Exposes an actor which the remote side can interact with
call a method on an actor by id.
Expire a previously exposed actor.
Having a separate emitter differentiates from remote events.
Interface used by actor system. All optional. Methods called by remote side can be synchronous or return a promise.
Returns value of property on actor at point at which message is received. Serialized/deserialized as JSON.
Transports are very simple. They call a RpcPair's .incoming
method with incoming messages, and use .setSend
to inform a pair that it can send messages via this transport.
That's it! Take a look at transports/streamTransport.js
to see a transport that lets RPCjs work with TCP/UDP/HTTPS or whatever streams you like, however crazy the chain of compression, encryption etc in that stream is!
To see debug messages, set the DEBUG env var:
DEBUG=rpcjs node yourApp.js