title: Sending a message to a client via its socket.id
body: <p>I can only seem emit messages to users when their socket id has been directly stored within the io.sockets.on('connect') function. I don't know why it doesn't work when trying to store their socket id after they have logged in.</p>
  
  <p><strong>Working:</strong></p>
  
  <pre><code>  var clients = {};
    /** A new socket connection has been accepted */
    io.sockets.on('connection', function (socket)
    {
      //Used to access session id
      var hs = socket.handshake;            
          clients[socket.id] = socket; // add the client data to the hash   
          users['brownj2'] = socket.id; // connected user with its socket.id
  
      socket.on('user-login', function(user){
        if(!users[username]){
          //users[username] = socket.id; // connected user with its socket.id
        }
      })
  
      socket.on('page-load', function(pid)
      {
        if(users['brownj2'])
        {        
          clients[users['brownj2']].emit('hello');
        }
        else
        {
          console.log("NOT FOUND BROWNJ2");
        }
      }
    }
  </code></pre>
  
  <p><strong>Not working:</strong></p>
  
  <pre><code>  var clients = {};
    /** A new socket connection has been accepted */
    io.sockets.on('connection', function (socket)
    {
      //Used to access session id
      var hs = socket.handshake;            
          clients[socket.id] = socket; // add the client data to the hash            
  
      socket.on('user-login', function(user){          
        if(!users['brownj2']){
          users['brownj2'] = socket.id; // connected user with its socket.id
        }
      })
  
      socket.on('page-load', function(pid)
      {
        if(users['brownj2'])
        {        
          clients[users['brownj2']].emit('hello');
        }
        else
        {
          console.log("NOT FOUND BROWNJ2");
        }
      }
    }
  </code></pre>
  
  <p><strong>JavaScript Client-side code snippet</strong></p>
  
  <pre><code>var socket = io.connect('');
  socket.on("hello", function(){
    alert("Hi Jack");
    console.log("WEBSOCKET RECIEVED");
  })
  </code></pre>
  
  <p><strong>Solution: Thanks @alessioalex</strong>
  I have had to remove the reference to socket.io from the login page, and add the following into io.sockets.on('connection')</p>
  
  <pre><code>var hs = socket.handshake;
  
  sessionStore.get(hs.sessionID, function(err, session){
    console.log("SESSION: " + util.inspect(session));
  
    clients[socket.id] = socket; // add the client data to the hash
    validUsers[session.username] = socket.id; // connected user with its socket.id
  })
  </code></pre>