0.0.2 • Published 2 years ago

irpc v0.0.2

Weekly downloads
1
License
ISC
Repository
-
Last release
2 years ago

irpc

RPC over the IPC channel

Client API

load the module

var irpc = require("irpc");

activate the client

irpc.client(process);

register commands

process.rpc.nop = (arg,callback) => callback("OK", null);
process.rpc.echo = (arg,callback) => callback("OK", arg);
process.rpc.delayed = (arg,callback) => setTimeout(() => callback("OK", null), 3000);

process.rpc.do_something = (arg,callback) => {
	var rc;
	try {
		rc = do_something(arg);
	}
	catch (err) {
		callback("ERR", "exception");
		return;
	}
	callback("OK", rc);
}

The first argument to callback is the status code.

  • OK Success response from the peer.
  • ERR Error response from the peer.
  • BAD Unknown command. Error response from the client library.
  • XERR An exception occured. Error response from the client library.
  • BROKEN Connection lost. Error response from the server library.
  • TIMEOUT Command timed out. Error response from the server library.

Only the OK response is a positive response.

Server API

load the module

var irpc = require("irpc");

fork a worker

var worker = cluster.fork();

active the server on the worker instance

irpc.server(worker);

call a remote procedure

worker.rpc([timeout,] cmd [,arg]... [,callback]);

for example:

worker.rpc("do_something");
worker.rpc("do_something_and_tell_me", (err,arg) => ___);
worker.rpc(3000, "echo", "1", "2", (err,arg) => console.log("got a response:", err, arg));

Wire Protocol

The request message is an array with three items:

[ref,cmd,arg]
  • ref is a reference identifier (string or number)
  • cmd is the command (string)
  • arg is the argument (anything that can be stringified to JSON)

The response message is an array with three item:

[ref,err,arg]
  • ref is the reference identifier from the request (string or number)
  • err is the status code (string or number)
  • arg is the argument (anything that can be stringified to JSON)

ChangeLog

0.1.0 (2018-10-24)

  • initial version
  • supports Node.js 11.x 10.x 8.x 6.x 4.x 0.12.x 0.10.x and io.js 3.x 2.x 1.x

•••

0.0.2

2 years ago

0.1.0

6 years ago

0.0.1

6 years ago