It is an interactive class based on the process communication between `master`, `Worker` and `Agent`.
npm install ipc-messageIt is an interactive class based on the process communication between master, Worker and Agent.
``bash`
npm install --save ipc-message
`javascriptagent
const IPCMessage = require('ipc-message');
module.exports = class NodeBase extends IPCMessage {
constructor() {
// If it is a 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
}
}
}
`
We can receive messages from other processes through the event message.
`javascript[${this.type}] Receive Message:
const IPCMessage = require('ipc-message');
module.exports = class NodeBase extends IPCMessage {
constructor() {
super();
this.on('message', msg => {
console.log(, msg);`
});
}
}
We send data through the send method.
`javascript`
this.send(to, action, data);
Introduction of parameters:
- to Array|String|Number Which process to send data to: master workers agents * String
- action 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.
`javascript`
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 of Agent.Object
- agentobject 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.
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.
IPC Message is MIT licensed.