title: Node JS : socket.io - handling multiple connection
body: <p>I am a newbie in using socket.io in Node JS - however I have coded a client (web page) / server program to render some statistics data at client side.
  The requirement is like – to render a box statistics (out of many). Since the user can open multiple browser windows - we have a scenario where one box data can be requested by many times:</p>
  
  <p><a href="http://www.tool.com?boxname=box1" rel="nofollow">http://www.tool.com?boxname=box1</a></p>
  
  <p>As such I want to achieve spawning one job for multiple requests for same box. Below is the logic I have implemented to meet the requirement: </p>
  
  <p>Client Side:</p>
  
  <ol>
  <li><p>Establishes a connection to server creating a data channel:</p>
  
  <p>socket.on(boxname.toLowerCase(), function (data){
  }</p></li>
  </ol>
  
  <p>So whenever there is a data in boxname event I receive, parse the data and render it.</p>
  
  <p>Server Side</p>
  
  <ol>
  <li><p>First call an expect script to copy a script to run within an infinite loop on a box.</p></li>
  <li><p>On successful copying of the script in the box - invoke another expect script to start its execution:</p>
  
  <p>exports.ServiceMonitor = function(boxName, res) {</p></li>
  </ol>
  
  <p>Step 1. </p>
  
  <p>I need to copy a script as many times request comes else I would not be able to enter ‘exit’ event of this spawned process where I start the script execution as mentioned below:</p>
  
  <pre><code>var child = spawn('./scripts/copyscriptonbox.sh' , [boxName.toLowerCase(), getURL(boxName)]);
  </code></pre>
  
  <p>In the later part of the code I keep adding box names to boxnames variable which is declared global. So on a new boxname request - I search for number of occurrences of boxname in boxnames variable. If the count is 1 or more it would mean to me that a script is already running on that box:</p>
  
  <pre><code>        var noofbox = boxnames.split(boxName.toLowerCase()).length - 1;
  
          child.on('exit', function (code) {
                  logger.info('child stream exit: ' + code);
                  if(code == 0)
                  {
                          boxNames +=  boxName.toLowerCase();
                          logger.info('Box name added: ' + boxNames);
                          res.render('boxpage', {}); //render a web page
                          io.sockets.on('connection', function (socket) {
              logger.info('Connected to box :' + boxName);
              if(noofbox &lt;= 0)            
                             schild = spawn('./scripts/boxmon.sh', [boxName.toLowerCase(), getURL(boxName)]);
                          schild.on('exit', function (code) {
                                  logger.info('schild stream exit.');
                           });
                          schild.stderr.on('data', function (data) {
                                  logger.info('stderr: ' + data);
                          });
                          schild.stdout.on('data', function (data) {
                                //generate response
                          });
                          socket.on('disconnect',function(){logger.info('Disconnect Killed');ServiceMonitorEnd(boxName.toLowerCase());});
                          });
                  }
          });
  }
  </code></pre>
  
  <p>The issue is that if in two browser window I access URL : www.tool.com?boxname=box1 - first time I get the log only once (Connected to box : box1) but second time I get the same logs 2 times where as I was expecting it to be one time - I mean as many request comes in after the first one the logs gets printed that many times – if 5 then log gets printed for 1(first time)+2 (second time)+3(third time)+4 (fourth time)+5 (fifth time)? I understand that when ‘connection’ event is called for x times then it enters that many times for each connection.
  How can I make the 'connection' event on the socket.io once for each request only?</p>