Fork() & Messaging between parent child processes in NodeJS

NodeJS, in very likeness to C/C++ offers to fork multiple processes from a parent process. Each of these fork() process can essentially run a separate flow of execution written in a different js file altogether. So we can have two files parent.js and child.js each running as separate processes. We are dealing with multiple processes and not multiple threads!, so it makes sense to think that OS is going to assign them to different cores for processing and so its absolutely parallel execution of logic in terms of an execution architecture.

Communicating between parent and child

There is a “message” event available for each process object and send() function available for sending. The use of these together will let us communicate between processes. A simple example is given below with output:

Leave a Reply