node-simple-ipc
v1.0.0
Published
A Node.Js module for local Inter Process Communication
Downloads
9
Maintainers
Readme
node-simple-ipc
A library created on top of Node.js IPC, which simplify communication between master and child processes via RPC and Events.
Install
Install via npm,
npm install node-simple-ipc
Docs
See wiki pages.
Quick Example
master.js - the master process who spawn child process:
const { fork } = require('child_process');
const { NodeSimpleIpc } = require('node-simple-ipc');
// Spawns the child process
const childProcess = fork('./child.js');
// Creates a new instance of NodeSimpleIpc
const rpc = new NodeSimpleIpc(childProcess);
// Request "get_time" endpoint and receive the response
rpc.act('get_time').then((time) => console.log('current time:', time));
// Request "math_add" endpoint and receive the math result
rpc.act('math_add', [5, 10, 100]).then((result) => {
console.log('math result:', result);
});
// Register "ping_master" RPC on master side
rpc.add('ping_master', () => 'pong from master');
child.js - the child process spawned from the master:
const { NodeSimpleIpc } = require('node-simple-ipc');
// Creates a new instance of NodeSimpleIpc
const rpc = new NodeSimpleIpc(process);
// Register "get_time" endpoint
rpc.add('get_time', () => Date.now());
// Register "math_add" endpoint
rpc.add('math_add', (numbers) => {
return numbers.reduce((a, b) => a + b, 0);
});
// Request "ping_master" endpoint and receive the math result
rpc.act('ping_master').then((result) => {
console.log('master ping response:', result);
});
Console output:
master ping response: pong from master
current time: 1648818853923
math result: 115
To do:
- More code examples.
- Implement graceful exit.
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
Donation
Give me a Star if you like it. 😊
License
All contents of this package are licensed under the MIT license.