title: Socket.io room feature to create game tables on demand
body: <p>I am using JS and socket.io to make a multiplayer game.</p>
  
  <p>The game player limit is ranging from 2-5.</p>
  
  <p>What I want to achieve is :</p>
  
  <p>1: On the client browser there is a dropdown list displaying all the game table  names and its max player limit. A player can join any room by click on it.</p>
  
  <p>2: Once the table player limit is reached, the room cannot be joined again.</p>
  
  <p>3:players in each room can play their own game, chat in their own channel without being distracted.</p>
  
  <p>In my current design there is a static room object and in the room project there is an array of pre-created tables object. The table object has many of the properties and methods that a game would need.</p>
  
  <p>Server side room and table object:</p>
  
  <pre><code>function Room(name) {  
  this.players = [];
  this.tables = [];
  };
  
  function Table(tableID){  
  this.id = tableID;
  this.players = [];
  this.maxPlayerLimit = 4;
  }; 
  </code></pre>
  
  <p>I can pre-created amount number of tables and push these tables object to room's tables array.</p>
  
  <pre><code>Room.prototype.createTables = function(amount) {
  var tableList = [];
  for(var i = 0; i &lt; amount; i++){
      var table = new Table(i+1);
      table.setName("Table" + (i + 1));
      tableList.push(table);
  }
  return tableList;
  };
  </code></pre>
  
  <p>client side when they click on the dropdown:</p>
  
  <p>The drop down looks like <a href="https://jsfiddle.net/1oyy98uo/" rel="nofollow noreferrer">https://jsfiddle.net/1oyy98uo/</a></p>
  
  <p><code>socket.emit("connectToTable", {tableID:TableID,tableName: tableName,limit:limit});</code> //these info is based on their table selection in the dropdown</p>
  
  <p>When a player connect to server I push them to the static room player array.</p>
  
  <p>when they choose a table, I do</p>
  
  <pre><code>socket.on('connectToTable',function(data) {
    var player = room.player(socket.id)// (room method to get the socket player)
    var table = room.table(data.tableID); //(room method to get the table)
    table.Name(data.tableName)//(table method to set name)
    table.maxPlayerLimit = Number(data.limit)
    if (tableisAvailable){
     table.join(player)//table method to add player to its list
    if(table.players.length===maxPlayerlimit){
     init the game
     }else{
     client.emit("table is full")//then the client would know to go elsewhere
     }
     }
     })
  </code></pre>
  
  <p>My design fulfills all the goals. but it is done in such a hard-coded way. <strong>The pre-created table might be too many or too little.</strong> The only way to solve this with my current design is to create more rooms than expected.</p>
  
  <p>I am not sure if I should still stick with my current design or socket.io room feature can provide a better solution. (creating game tables on demand)</p>
  
  <p>But there is a flaw in the on-demand scenario:</p>
  
  <p>players make requests to join a room(table), once the requests reach to a certain limit (like 4) the server will join these players. This way the server will only create as many rooms(tables) as necessary. <strong>But these queued-up players are random players,  they may not necessarily want to play with each other. If players want to play with their friends, this will be a problem.</strong></p>