title: Socket.io receiving data from multiple sockets in Node
body: <p>I started developing in Node just a couple of days ago. I'm currently developing an online game that uses Node as server-side application. This application simply provides the .html and .js files to the client and handles the logic of the game.
  I'm using Socket.io to exchange data with the clients. When a new client connects to the server through Socket.io the program looks for a room (array of objects containing the sockets for the two users and the game object that handles the logic) that has an empty space and places the user (registering the socket) in it. 
  I've been able to correctly send to each client the position of the sprites, but I have a problem with retrieving data from each socket: If I try to send some data from the client to the server, that data flows through only one of the two sockets, no matter which client is sending that.</p>
  
  <p><strong>Initializing the room array</strong></p>
  
  <pre><code>for(var i = 0; i &lt; roomsNumber; i++) {
    //player1 and player 2 are going to store the sockets
    rooms[i] = { player1 : null, player2 : null, game: null};
  }
  </code></pre>
  
  <p><strong>Socket.io handling the connections</strong></p>
  
  <pre><code>var io = require('socket.io')(server);
  
  io.on('connection', function(socket){
    console.log("We have a new client: " + socket.id);
    insertPlayer(socket);
  
    socket.on('disconnect', function() {
      console.log("Client has disconnected: " + socket.id);
      removePlayer(socket);
    });
  });
  </code></pre>
  
  <p><strong>Adding the clients to rooms</strong></p>
  
  <pre><code>function insertPlayer(socket) { //Check for an empty spot in a room
    for(var i = 0; i &lt; rooms.length; i++) {
      if(!rooms[i].player1) {
        rooms[i].player1 = socket; //Register the socket to the room
        checkRooms();
        return;
      }
      if(!rooms[i].player2) {
        rooms[i].player2 = socket; //Register the socket to the room
        checkRooms();
        return;
      }
    }
  }
  </code></pre>
  
  <p><strong>Creating the Game object when a room if full (and destructing it when a player leaves)</strong></p>
  
  <pre><code>function checkRooms() { //Checks if a room is full to initialize the game
    for(var i = 0; i &lt; rooms.length; i++) {
      if(rooms[i].player1 &amp;&amp; rooms[i].player2 &amp;&amp; !rooms[i].game) {
        rooms[i].game = new Game([rooms[i].player1, rooms[i].player2]); //The constructor passes the sockets
        console.log('New game started');
      }
      if((!rooms[i].player1 || !rooms[i].player2) &amp;&amp; rooms[i].game) {
        rooms[i].game.endGame();
        rooms[i].game = null;
        console.log('Game stopped');
      }
    }
  }
  </code></pre>
  
  <p><strong>Receiving data in the Game class</strong></p>
  
  <pre><code>constructor(sockets) {
    this.sockets = sockets;
    this.snakes = [];
    var self = this;
  
    for(var i = 0; i &lt; this.sockets.length; i++) {
      this.snakes[i] = new Snake(i*100 + 100, 200);
      var currentSnake = this.snakes[i];
      var currentSocket = this.sockets[i];
  
    currentSocket.on('keyPressed', function(data) {
      currentSnake.updateDirection(data); //Calls a Game method that updates the direction of the sprite 
      console.log(currentSocket.id + " - " + data);
    });
  }
  </code></pre>