title: Nodejs real time online user counter
body: <p>I have a website and implemented a real time online user counter using nodejs and socket.io. Here is the code:</p>
  
  <pre><code>$io.on('connection', function (socket) {
  
      var $ip_address = socket.handshake.address;
      if ($ips_connected.hasOwnProperty($ip_address)) {
          $ips_connected[$ip_address]++;
      } else {
          $ips_connected[$ip_address] = 1;
          $connected_users++;
  
  
          if ($connected_users &gt; $max_users)
              $max_users = $connected_users;
      }
  
      socket.emit('update', $connected_users);
  
      socket.on('disconnect', function () {
          if ($ips_connected.hasOwnProperty($ip_address)) {
              $ips_connected[$ip_address]--;
              if ($ips_connected[$ip_address] &lt;= 0) {
                  delete $ips_connected[$ip_address];
                  $connected_users--;
              }
          }
      });
  
      socket.on('update', function () {
          socket.emit('update', $connected_users);
      });
  
  });
  </code></pre>
  
  <p>As you can see, i do not consider multiple conections by the same ip address. The problem with this code is that Google Analytics Real Time data are showing a number smaller than that calculate by nodejs.</p>
  
  <p>What i need is a  explanation of someone who have already implemented something like this using nodejs to tell me if i can trust on this code or should i trust on Analytics real time counter. I'm showing to my users the number of users connected to my website at the moment using this code, so i dont want to display wrong numbers.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I forgot to say that analytics are showing numbers like 30 units less than my own counter.</p>