ipc-message
v1.0.10
Published
It is an interactive class based on the process communication between `master`, `Worker` and `Agent`.
Downloads
10
Readme
IPC Message
It is an interactive class based on the process communication between master
, Worker
and Agent
.
Install
npm install --save ipc-message
Usage
const IPCMessage = require('ipc-message');
module.exports = class NodeBase extends IPCMessage {
constructor() {
// If it is a `agent` type process, you need to set the parameter to `true`.
// super(true);
super();
// receive message from other processes.
this.on('message', msg => {
console.log(`[${this.type}] Receive Message:`, msg);
});
if (this.type === 'master') {
// do master ...
} else {
// do worker
}
}
}
Receive Message Event
We can receive messages from other processes through the event message
.
const IPCMessage = require('ipc-message');
module.exports = class NodeBase extends IPCMessage {
constructor() {
super();
this.on('message', msg => {
console.log(`[${this.type}] Receive Message:`, msg);
});
}
}
Send Message Function
We send data through the send
method.
this.send(to, action, data);
Introduction of parameters:
- to
Array|String|Number
Which process to send data to:master
workers
agents
*
- action
String
Data identification - data
*
data body
When we send data through the subprocess or the Agent
process, the master
process is transferred. For example, if we want to send the Agent
process to the sub process, we will first send it to the Agent
process through the master
process, and vice versa.
Regist Agent
const agentWorkerRuntimeFile = path.resolve(__dirname, 'agent.js');
const agent = ChildProcess.fork(agentWorkerRuntimeFile, null, {
cwd: process.cwd(),
stdout: process.stdout,
stderr: process.stderr,
stdin: process.stdin,
stdio: process.stdio
});
this.registAgent('agentname', agent);
this.registAgent(agentname, agentobject)
- agentname
String
The name ofAgent
. - agentobject
Object
Agent object.
No matter how many Agent
processes you open, you must use this method to register. This method will automatically bind the logic of sending and receiving messages from Agent.
Test
You can see how to write through the examples in the test folder, and you can test the class by using the npm run test
command.
License
IPC Message is MIT licensed.