title: What approach should I take for creating a "lobby" in Node.js?
body: <p>I have users connecting to a Node.js server, and when they join, I add them into a Lobby (essentially a queue).  Any time there are 2 users in the lobby, I want them to pair off and be removed from the lobby.  So essentially, it's just a simple queue.</p>
  
  <p>I started off by trying to implement this with a Lobby.run method, which has an infinite loop (started within a <code>process.nextTick</code> call), and any time there are more than two entries in the queue, I remove them form the queue.  However, I found that this was eating all my memory and that infinite loops like this are generally ill-advised.</p>
  
  <p>I'm now assuming that emitting events via <code>EventEmitter</code> is the way to go.  However, my concern is with synchronization.  Let's assuming my Lobby is pretty simple:</p>
  
  <pre><code>Lobby = {
    users: []
  
  , join: function (user) {
      this.users.push(user);
      emitter.emit('lobby.join', user);
    }
  
  , leave: function (user) {
      var index = this.users.indexOf(user);
      this.users.splice(index, 1);
      emitter.emit('lobby.leave', user);
    }
  };
  </code></pre>
  
  <p>Now essentially I assume I want to watch for users joining the lobby and pair them up, maybe something like this:</p>
  
  <pre><code>Lobby = {
    ...
  
  , run: function () {
      emitter.on('lobby.join', function (user) {
        // TODO: determine if this.users contains other users,
        //       pair them off, and remove them from the array
      });
    }
  }
  </code></pre>
  
  <p>As I mentioned, this does not account for synchronization.  Multiple users can join the lobby at the same time, and so the event listener might pair up a single user with multiple other users instead of just one.</p>
  
  <p>Can someone with more Node.js experience tell me if I am right to be concerned with this event-based approach?  Any insight for improvement on this approach would be much appreciated.</p>