title: Unable to emit to a specific room and client (Socket.io 2.0.2)
body: <p>I am using Socket.io to create a multiplayer game. I have used a generated numerical code to dynamically create and join rooms.</p>
  
  <p>My issue comes about emitting to specific rooms or even specific clients. I am unable to emit to a room (<code>io.in(room).emit('event')</code> or <code>io.to(room).emit('event')</code> <em>They are synonyms</em>). I am however able to <code>socket.emit('event')</code> between the server and client just fine. No errors. Simply nothing happens when I use anything but <code>socket.emit()</code>, <code>io.emit()</code>, and <code>socket.on('',function(){ this.emit(); })</code>.</p>
  
  <p>The reason I have to emit to specific rooms is to update all clients in their rooms when a new client has joined. <em>(I had tried emitting to each socket.id in each room but that does not work)</em>.</p>
  
  <p><a href="https://i.stack.imgur.com/PmDPp.png" rel="nofollow noreferrer">browser debugger tracking server emitted events</a></p>
  
  <p>I have uploaded all the code I have used in my Node.js server in hopes that someone can see the error in my program. I am new to Socket.io and I am not sure about the validity of how I have set up my dynamic rooms.</p>
  
  <p>The room event that does not work is: <strong>connectToRoom</strong></p>
  
  <p><strong>Server</strong></p>
  
  <pre><code> var express = require('express');
   var app = express();
   var http = require('http').Server(app);
   var io = require('socket.io')(http);
  
   app.get('/',function(req, res) {
       res.sendFile(__dirname + '/client/index.html');
   });
   app.use('/client',express.static(__dirname + '/client'));
   http.listen(3000, function(){
       console.log('listening on localhost:3000');
   });
  
  io.on('connection', function(socket){
      socket.id = Math.random();
      SOCKET_LIST[socket.id]=socket;
  
      socket.on('create',function(){
          var thisGameId = ( Math.random() * 100000 ) | 0;
          roomNo+=1;
          roomArr[thisGameId]=thisGameId;
          this.emit('createResponse', {gameId: thisGameId, mySocketId: socket.id});
          this.join(thisGameId.toString());
      });
  
      socket.on('joinRoom',function(data){
          //playerJoinGame(data);
          //io.sockets.in(data.roomKey.toString()).emit('connectToRoom', "You are in room no. "+data.roomKey);
          //socket.to(data.roomKey.toString()).emit('connectToRoom', "You are in room no. "+data.roomKey);
          if( io.nsps['/'].adapter.rooms[data.roomKey]!== undefined ){
              socket.join(data.roomKey.toString());
              SOCKET_LIST[socket.id].username = data.username;
              this.emit('joinRoomResponse',{
                  roomKey:data.roomKey
              });
          }
          if(io.nsps['/'].adapter.rooms[data.roomKey]=== undefined){
              this.emit('joinError',{
                  message: "This room does not exist."
              });
          }
      });
      socket.on('updateRoom',function(data){
          var clients=io.sockets.adapter.rooms[data.roomKey].sockets;
          var clientsArr=Object.keys(clients);
          for (var clientId in clientsArr ) {
              io.sockets.connected[clientsArr[clientId]].emit('connectToRoom', {
                  roomKey:data.roomKey,
                  username:data.username
              });
          }
  
          io.sockets.in(data.roomKey).emit('connectToRoom', {
              roomKey:data.roomKey,
              username:data.username
          });
      });
  
      socket.on('disconnect',function(){
      delete SOCKET_LIST[socket.id];
      });
  });
  </code></pre>
  
  <p><strong>Client</strong></p>
  
  <pre><code>var socket = io();
  var roomKey,username,mySocketId;    
  var optionDiv = document.getElementById('optionDiv');
  var optionDivCreate = document.getElementById('optionDiv-create');
  var optionDivJoin = document.getElementById('optionDiv-join');
  
  var prepDiv = document.getElementById('prepDiv');
  var createDiv = document.getElementById('create-Div');
  var lobbyDiv = document.getElementById('lobbyDiv');
  var createRoomKey = document.getElementById('create-roomKey');
  var createPlayers = document.getElementById('create-players');
  var joinForm = document.getElementById('join-form');
  var joinForm_roomKey = document.getElementById('join-roomKey');
  var joinForm_username = document.getElementById('join-username');
  var joinForm_submit = document.getElementById('join-form-submit');
  var gameDiv = document.getElementById("gameDiv");
  
  optionDivCreate.onclick=function(){
      socket.emit('create');
  };
  optionDivJoin.onclick=function(){
      optionDiv.style.display='none';
      prepDiv.style.display='inline-block';
      joinForm.style.display='inline-block';
  };
  socket.on('createResponse',function(data){
      roomKey = data.gameId;
      mySocketId = data.mySocketId;
      optionDiv.style.display='none';
      prepDiv.style.display='inline-block';
      createDiv.style.display='inline-block';
      createRoomKey.innerHTML = roomKey;
  });
  
  joinForm_submit.onclick= function(){
  
  };
  joinForm.onsubmit = function(e){
      e.preventDefault();
      roomKey = joinForm_roomKey.value;
      username = joinForm_username.value;
      socket.emit('joinRoom',{
          roomKey:roomKey,
          username:username
      });
      joinForm_roomKey.value='';
      joinForm_username.value='';
  };
  socket.on('joinRoomResponse',function(data){
      optionDiv.style.display='none';
      createDiv.style.display='none';
      prepDiv.style.display='none';
      lobbyDiv.style.display='inline-block';
      socket.emit('updateRoom',{
          roomKey:roomKey,
          username:username
      });
  });
  socket.on('connectToRoom',function(data){
      socket.emit('debug');
      //createPlayers.innerHTML = "&lt;br /&gt;"+data.username;
      alert("triggered");
  });
  socket.on('joinError',function(data){
      alert(data.message);
  });
  </code></pre>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;!--
  To change this license header, choose License Headers in Project Properties.
  To change this template file, choose Tools | Templates
  and open the template in the editor.
  --&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Prototype&lt;/title&gt;
          &lt;meta charset="UTF-8"&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div id="optionDiv" style=""&gt;
              &lt;button id="optionDiv-create"&gt;Create Game&lt;/button&gt;&lt;br /&gt;
              &lt;button id="optionDiv-join"&gt;Join Game&lt;/button&gt;
          &lt;/div&gt;
          &lt;div id="prepDiv" style="display:none;"&gt;
              &lt;div id="create-Div" style="display:none;"&gt;
                  Room Key:&lt;br /&gt;
                  &lt;h1 id="create-roomKey"&gt;&lt;/h1&gt;
                  &lt;h1 id="create-players"&gt;&lt;/h1&gt;
              &lt;/div&gt;
              &lt;form id="join-form" style="display:none;"&gt;
                  Username:&lt;br /&gt;
                  &lt;input id="join-username" type="text" style="width:500px"&gt;&lt;/input&gt;&lt;br /&gt;
                  Room Key:&lt;br /&gt;
                  &lt;input id="join-roomKey" type="text" style="width:500px"&gt;&lt;/input&gt;&lt;br /&gt;
                  &lt;button id="join-form-submit"&gt;Join&lt;/button&gt;
              &lt;/form&gt;
          &lt;/div&gt;
          &lt;div id="lobbyDiv" style="display:none;"&gt;
              You are in room:&lt;br /&gt;
              &lt;h1 id="join-roomKey"&gt;&lt;/h1&gt;&lt;br /&gt;
              Players in room:&lt;br /&gt;
              &lt;h1 id="join-players"&gt;&lt;/h1&gt;
          &lt;/div&gt;
          &lt;div id="gameDiv" style="display:none;"&gt;
              &lt;div id="gameDiv-canvas"&gt;
                  &lt;canvas id="ctx" width="500" height="500" style="border:1px solid #000000;"&gt;
                  &lt;/canvas&gt;
              &lt;/div&gt;
              &lt;div id="gameDiv-chat"&gt;
                  &lt;div id="chat-text" style="width:500px;height:100px;overflow-y:scroll"&gt;
                      &lt;div&gt;
                          Hello!
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;form id="chat-form"&gt;
                      &lt;input id="chat-input" type="text" style="width:500px"&gt;&lt;/input&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;!--&lt;script src="https://cdn.socket.io/socket.io-1.4.5.js"&gt;&lt;/script&gt;
          &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.2/socket.io.js"&gt;&lt;/script&gt;--&gt;
          &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
          &lt;script src="/client/js/client.js"&gt;&lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>