Non-blocking cross-process method call based on IPC for Node.js.
$ npm install --save node-ipc-call
const { Caller } = require('node-ipc-call');
const invoker = Caller.fork('./foo.js');
await invoker.invoke('sleep', 1000);
await invoker.invoke('max', [1, 2, 3]); // 3
invoker.destroy();
// ./foo.js
const { Callee } = require('node-ipc-call');
const callee = new Callee();
// async method
callee.register(async function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
});
// sync method
callee.register(function max(...args) {
return Math.max(...args);
});
callee.listen();
Added in: v0.0.1
Added in: v0.0.1
Caller.fork()
take the same parameters of child_process.fork().
Returns .
Added in: v0.0.1
Returned by Caller.fork()
, you should make remote function calls via Invoker
.
Added in: v0.0.1
name
{String} The remote function name.args
{Array} Arguments passed to the remote function.options
{Object}timeout
{Number} In milliseconds. Default: 3000.
Returns <Promise>
.
Added in: v0.0.1
Closes the IPC channel to child process, and rejects all pending calls.
Added in: v0.0.1
Added in: v0.0.1
arg
{Function|Object|Array} The functions or an array of functions exposed to remote call.
Each function to be registered should have an unique name, register the same function multiple times is ok.
callee.register(function foo() {});
callee.register({ foo() {} });
callee.register([ foo, bar ]);
callee.register(() => {}); // Error
Added in: v0.0.1
Starts to listen for method calls from parent process.
Added in: v0.0.1
Stops listening method calls from parent process.
MIT