Multiple processes in nodejs and sharing ports

 Development, node.js  Comments Off on Multiple processes in nodejs and sharing ports
Mar 172012

Recently I have been doing alot of work with node.js. As part of that work, we used multiple processes spawned by the node main node process. I wanted to just write down and share some of my experiences.

Starting new processes in node is easy
Nodejs docs

Using the child_process module you have several options for creating child processes. These include spawn,
fork and exec.

fork :
duplicates your shell in a sub shell. So if node is running you can run new node code in this new process.

exec :
Normally executes in the current shell replacing the process space. in nodejs it runs the command in a sub shell. To run the command in the current process space you should use execFile

spawn :
duplicates and then execs. So is like calling fork (create new duplicate process) and then execFile (the command you want to run in that environment).


   var child = require('child_process');

   var forked = child.fork("./mynewscript.js");

if you were to open up your terminal now and run a ps -ef | grep node, you should see two processes running (assuming the code doesn’t run and immediately exit).
Another useful feature, is that the child and parent process can send messages to one another. Taking the above example you can add.

  forked.on('message', function(msg){
     console.log("parent received message" + msg);
  // send message to child
  forked.send({ hello: 'world' });

  //in mynewscript.js
  process.on('message', function(m) {
    console.log('CHILD got message:', m);

  process.send({ foo: 'bar' });

Starting new processes that listens on a shared port is easy

Cluster info nodejs
Node.js has a cluster module which is made for sharing ports with multiple processes. “The cluster module allows you to easily create a network of processes that all share server ports.”

A quick example:

 var cluster = require('cluster');

 var server = require('http').createServer(function (req,res){
     res.end("hello world");
 var workers = [];
    for(var i=0; i < 2; i++){
       var worker = cluster.fork();
    //forked process space also have server

 //restart on death of any sub process
 cluster.on('death', function (worker){
      for(var i=0; i < workers.length){
            var aworker = workers[i];
            if( === workers.splice(i);
     var regen = cluster.fork();

Points to bear in mind

With the clustering if you kill the parent process the child processes will not die and so will continue to serve. If you want to stop all child processes when you stop the parent process do something like:

     for(var i=0; i < workers.length; i++){

Also something to note is that from my testing, if you put the http server on the main process and also on the worker processes, the main process will always answer the request and the child processes will receive none of the requests.

© 2012 Craig Brookes Suffusion theme by Sayontan Sinha