Matias Salimbene
Matias Salimbene

Reputation: 605

Inquires about node process.send functioning and impact

These are a couple of conceptual concerns I've got. Node docs state about process.send:

If Node.js is spawned with an IPC channel, the process.send() method can be used to send messages to the parent process. Messages will be received as a 'message' event on the parent's ChildProcess object.

If Node.js was not spawned with an IPC channel, process.send will be undefined.

What does it mean to be "spawned with an IPC channel" or not, should I care about it or is it something that OS will handle?

Also, reading node docs I got a minimum understanding of what is going in the code below. However, I do not get "running in a fork". Is it meaningful to perform this validation?

app.listen(port, () => {
  sendBootStatus("ready");
});

function sendBootStatus(status) {
  // don't send anything if we're not running in a fork
  if (!process.send) {
    return;
  }
  process.send({ boot: status });
}

Thanks in advance, happy hollydays.

Upvotes: 1

Views: 827

Answers (1)

bdew
bdew

Reputation: 1330

You start a child process with child_process.fork, code running in such a process can use process.send to send messages to the parent process.

The check is meaningful if you want to run the same module in main/child process, if so you can do

if (process.send) {
    // do whatever you need done in child process
} else {
    // this is main process spawn your child process(es) and do other setup
}

Upvotes: 1

Related Questions