Communicating between NodeJS processes

Intro : why create child processes in NodeJS

When running an application in NodeJS, it’s single threaded, and will only utilise a single core.

Many tasks to a single node process

When performing cpu-intensive or a great number of tasks, you may see this impacting performance, and see runtime/respond-time increase.

If your NodeJS Application has 100% cpu-usage and is taking a long time to complete or slow to respond, this can be improved by dividing the work to be done, and spreading it over multiple processes.

Many tasks distributed to multiple worker processes

Managing these processes and communicating between them can be a little daunting at first. But it can be easy to implement, especially if your code is async already.

Creating NodeJS child processes

NodeJS provides an package called child_process with utilities for spawning processes.

If you want run a new process running a NodeJS script you’ll want to use fork. Fork can only spawn a new NodeJS processes. You give it a javascript file to execute.

By default the child console/output will be inherited from the parent.
Which means you will see all the output in the terminal from both the parent and child processes.

Run a child_process silently

A simple way to just create a silent child process is to pass a options object and set the `silent` property:

Communicate to a child_process

To communicate with the child process we need to enable ipc — inter process communication. To do this we need to add list of input/output options.

Read more about the input/output options on nodejs.org.

This will enable process.send() and process.on('message') in the child process. These can be used to communicate between the 2 processes.

Creating any type of sub-process from NodeJS

Another utility provided from child_process is spawn. Spawn can be used to create a process from any command.

The command being executed here is node, but this can be any command for example a live transpiler like babel-node.

Communicate between 2 unrelated node processes

If you have two independent NodeJS processes running and want them to communicate, this can be done reliably using a npm package: node-ipc

It works by creating a global pub-sub system, which from any NodeJS process you can subscribe and publish on.

1 Process should start the pub-sub server, which other processes can connect to.

Making use of all cpu-cores for a single NodeJS process : clustering

child_process can also be used for clustering: running multiple instances of the same program and balancing a large workload over these processes.

In NodeJS 7 a new api was added: cluster and this makes creating a server that utilises all cores a breeze:

Workers can communicate via ipc with each other or with the entire cluster using the message event.

In closing

I hope this helped you. If you need more detailed information about child_process you can visit: https://nodejs.org/api/child_process.html.

Read more about NodeJS cluster here: https://nodejs.org/docs/latest/api/cluster.html#cluster_cluster