Package Exports
- node-simple-ipc
- node-simple-ipc/dist/main.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-simple-ipc) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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-ipcDocs
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: 115To 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.