title: node.js socket.io app - How to kick someone from chat room?
body: <p>So I have chat app in which only two users can be in a room at a time. </p>
  
  <p>Please note that clientInfo is a global variable and anytime a new user joins a room, an object variable is passed to the server with username and room name. Then this code is executed</p>
  
  <pre><code>socket.on('joinRoom', function(userDetails){
  
    clientInfo[socket.id] = userDetails; //variable contains name and room joined. 
    socket.join(userDetails.room);
  }
  </code></pre>
  
  <p>I added a feature where a user can type 'kick' as the chat message which will call this function:</p>
  
  <pre><code>function kickUser(socket){           
  
    socket.leave(clientInfo[socket.id].room);
    delete clientInfo[socket.id];
  
  }
  </code></pre>
  
  <p>the above code works fine but how can i change the function to kick the other user in the room? I tried the code below but I still ended up kicking myself out. I take it that socket.leave() doesn't care about the socket id but it just uses the room name and the person who called the function.</p>
  
  <pre><code>function kickUser(socket){
  
    var info = clientInfo[socket.id];  
  
    if (typeof info == 'undefined'){    
      return;
    }
  
     Object.keys(clientInfo).forEach(function(socketId){          
       if(info.room == clientInfo[socketId].room) {
          if (info.name != clientInfo[socketId].name){          
            socket.leave(clientInfo[socketId].room);
            delete clientInfo[socketId];  
          } 
        }
     });  
  }
  </code></pre>