Communicating between NodeJS processes

Intro : why create child processes in NodeJS

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

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

Communicate to a child_process

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

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

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

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

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store