1.0.10 • Published 6 years ago

ipc-message v1.0.10

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

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 of Agent.
  • 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.

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago