node-ipc-call

0.0.5 • Public • Published

node-ipc-call

version downloads license Travis Coverage

Non-blocking cross-process method call based on IPC for Node.js.

$ npm install --save node-ipc-call

Usage

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();

API

Class: Caller

Added in: v0.0.1

Caller.fork(modulePath, args, options)

Added in: v0.0.1

Returns .

Class: Invoker

Added in: v0.0.1

Returned by Caller.fork(), you should make remote function calls via Invoker.

invoker.invoke(name, args[, options])

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>.

invoker.destroy()

Added in: v0.0.1

Closes the IPC channel to child process, and rejects all pending calls.

Class: Callee

Added in: v0.0.1

callee.register(arg)

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

callee.listen()

Added in: v0.0.1

Starts to listen for method calls from parent process.

callee.destroy()

Added in: v0.0.1

Stops listening method calls from parent process.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i node-ipc-call

Weekly Downloads

0

Version

0.0.5

License

MIT

Unpacked Size

18.9 kB

Total Files

6

Last publish

Collaborators

  • micooz