title: Socket.IO - setInverval every two minutes (Node JS)
body: <p>I have an event in socket.io which broadcasts how many users are online (based on who is logged in) to the user.</p>
  
  <p>What I think should be happening is that I ask the server to query the database <strong>once</strong> every two minutes using <code>setInterval</code>, and a rowset is returned, then emitted to the client.</p>
  
  <p>What is happening though, is that <em>for each</em> user connected to the socket, it is calling the database on the server and pushing to the client.</p>
  
  <p>I'm uncertain why this is doing this - I'd understand if I was asking it to do this from the client, but as it's the server emitting the event <em>to</em> the client, why would it be doing this several time for each user connected?</p>
  
  <p>Thanks</p>
  
  <pre><code>io.on("connection", function(socket) {
  
    // Update online users' every two minutes
    setInterval(function() {
  
      var roomNum = 0;
      var outObj  = {};
  
      model.get_online_users(function(err, rowset) {
  
        // Loop thorugh the rowset, set the roomNum and built up out_obj to output
        io.to("room_number:"+roomNum).emit("online-users", outObj);
  
      }); // End `model callback`
  
    }, 120000); // End `get_online_users`
  
  }); // End `io.on("connection")`
  </code></pre>