https://github.com/micooz/node-ipc-call
Non-blocking cross-process method call based on IPC for Node.js
https://github.com/micooz/node-ipc-call
async ipc nodejs rpc
Last synced: 2 months ago
JSON representation
Non-blocking cross-process method call based on IPC for Node.js
- Host: GitHub
- URL: https://github.com/micooz/node-ipc-call
- Owner: micooz
- License: apache-2.0
- Created: 2018-11-18T15:22:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T15:20:30.000Z (about 3 years ago)
- Last Synced: 2025-09-30T19:52:22.795Z (6 months ago)
- Topics: async, ipc, nodejs, rpc
- Language: JavaScript
- Size: 659 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-ipc-call
[](https://www.npmjs.com/package/node-ipc-call)
[](https://www.npmjs.com/package/node-ipc-call)
[](https://github.com/micooz/node-ipc-call/blob/master/LICENSE)
[](https://travis-ci.org/micooz/node-ipc-call)
[](https://codecov.io/gh/micooz/node-ipc-call)
Non-blocking cross-process method call based on IPC for Node.js.
```
$ npm install --save node-ipc-call
```
## Usage
```js
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();
```
```js
// ./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
* `Caller.fork()` take the same parameters of [child_process.fork()](https://nodejs.org/dist/latest-v11.x/docs/api/child_process.html#child_process_child_process_fork_modulepath_args_options).
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 ``.
### 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.
```js
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