title: Create a list of Connected Clients using socket.io
body: <p>Here are 2 related questions. Posting them together makes more sense.</p>
  
  <p><strong>Question 1</strong></p>
  
  <p>I have a node.js app which emits an event out to all clients, and all current clients will respond with a <code>ready</code> emit. How can I create a list of all the clients that replied to the initial emit, and what kind of identification can be used to distinguish the clients?</p>
  
  <p><strong>Question2:</strong> </p>
  
  <p>What I am trying to do after  collect a list of connected clients, is to then access a MySQL database table of <code>N</code> number of rows and assign each client <code>X</code> rows each. These rows will be emitted back to their respective clients. How can this be done?</p>
  
  <p><strong>Current Code for Qn 1</strong></p>
  
  <p><strong>Node Code</strong></p>
  
  <pre><code>setInterval(function() {
      util.log('Checking for new jobs...');
      dbCheckQueue(function(results) {  // checks if there are new rows to "distribute" to clients
          if (results.length) {
              util.log(results.length + ' new jobs found.');
              io.sockets.emit('job_available');
          }
      });
  }, 10*1000);
  </code></pre>
  
  <p><strong>Client-side JS Code</strong></p>
  
  <pre><code>socket.on('job_available', function() {
                  console.log('Job Available.. Responding with Ready!');
                  socket.emit('ready');
              });
  
  io.sockets.on('connection', function(socket) {
      socket.on('ready', function() {
          // UPDATE N rows with client_id in column checkout.
          // Then SELECTS * from table where checkout = client_id
          getListings(client_id, function(listings) {
              socket.emit('job', listings);   // send jobs
          });
      });
  });
  </code></pre>
  
  <p><strong>Current Code for Qn 2</strong>
  <em>The code works for a single client, but how do I loop through all connected clients and perform the same updating of column and selecting of rows?</em></p>
  
  <pre><code>io.sockets.on('connection', function(socket) {
      socket.on('ready', function() {
          // UPDATE N rows with client_id in column checkout.
          // Then SELECTS * from table where checkout = client_id
          getListings(client_id, function(listings) {
              socket.emit('job', listings);   // send jobs
          });
      });
  });
  </code></pre>