1.3.5 • Published 2 years ago
interprocess-communication v1.3.5
Welcome to interpocess-communication documentation
ABOUT ME
Social networks
What's interprocess-communication?
Arousa interprocess-communication is a tool which helps you to send and recive messages between parent and child processes using promises.
Examples
Parent file
const {fork} = require("child_process");
const {Child} = require("interprocess-communication");
const child = new Child(fork("./child"));
child.send("Hello from parent!").then((message) => {
console.log(message.payload);
});
child.on("message", (message) => {
console.log(message.payload);
if (message.payload === "Hello from child!")
message.reply("Reply from parent!");
});
Child file
const {Parent} = require("interprocess-communication");
const parent = new Parent();
parent.send("Hello from child!").then((message) => {
console.log(message.payload);
});
parent.on("message", (message) => {
console.log(message.payload);
if (message.payload === "Hello from parent!")
message.reply("Reply from child!");
});
\(messageLifetime)
Parent constructor
Params
type | optional | description | |
---|---|---|---|
messageLifetime | number | ✓ | Time until a messages is deleted from cache |
\(childProcess, messageLifetime)
Parent constructor
Params
type | optional | description | |
---|---|---|---|
childProcess | ChildProcess Object | ✗ | Returned object from fork function |
messageLifetime | number | ✓ | Time until a messages is deleted from cache |
<Child|Parent>.send(payload, replying, event)
Sends a message to the parent or the child
Params
type | optional | description | |
---|---|---|---|
payload | any | ✗ | The content to send in the message |
replying | message id(number) | ✓ | Message id to reply |
event | string | ✓ | Event name |
.reply(payload, replying, event)
Replies the message
Params
type | optional | description | |
---|---|---|---|
payload | any | ✗ | The content to send in the message |
Special events
There are 3 special events launched once a message is sent.
- raw: Contains all messages json data. Its lauched every time a message is sent.
- unhandledReply: Contains a reply that could not be resolved.
- unhandledEvent: Contains a message that has no event (those messages are suposed to not appear).