title: Socket.io disconnects when refreshed?
body: <p>I am trying to build a live chat, I am using mongodb and socket.io to store the messages and users.</p>
  
  <p>When a new user is created that user is stored in the mongodb and in the socket object.</p>
  
  <p>If a user refreshes the page the user is removed from the socket object, meaning now in order for that person to get back in they have to create a new username and that generates a new socket.</p>
  
  <p>Here is what my server side code looks like</p>
  
  <pre><code>/*
  |--------------------------------------------------------------------------
  | Live socket communication with front end:
  |--------------------------------------------------------------------------
  |
  |
  */
  
  var users = {};
  
  io.sockets.on('connection', function (socket) {
  
      // Listen to a new user then emit the user to all clients
      socket.on('new user', function (data) {
  
          // Check mongodb to see if the user exists and emit proper message
          models.Message.findOne({username:data},function(err,user){
              if(err){
                  console.log('something went wrong')
              }
              else if(user){
                  socket.emit('username taken', 'something');
              }
              else{
                  socket.emit('create user', data);
                  socket.userName = data;
                  socket.connected = true;
                  users[socket.userName] = socket;
                  io.sockets.emit('user name', Object.keys(users));
              }
          });
  
      });
  
      socket.on('facebook id', function(data) {
  
          models.User.findOne({username:data.name}, function(err, user) {
              if (user) {
                  console.log('User already exists');
                  socket.userName = data.name;
                  socket.facebook_id = data.id;
                  socket.connected = true;
                  users[socket.userName] = socket;
                  io.sockets.emit('user name', Object.keys(users));
              } 
              else {
                  var newUser = new models.User({
                      username: data.name,
                      facebook_id: data.id
                  });
                  newUser.save(function(err, user) {
                      console.log('successfully inserted user/user: ' + user._id);
                  });
              }
          });
  
      });
  
      // Listen to a new message then emit the message to all clients
      socket.on('send message', function (data, callback) {
  
          io.sockets.emit('new message', {message: data, username: socket.userName, facebook_id: socket.facebook_id});
  
      });
  
      // Logic when client disconnects
      socket.on('disconnect', function (data) {
  
          if(!socket.userName) return;
          seeder.disconnect(socket.userName);
          delete users[socket.userName]
          io.sockets.emit('user disconnected', Object.keys(users));
  
      });
  
  });
  </code></pre>
  
  <p>You see in my disconnect I remove the socket from the users object. </p>
  
  <p>My question would be is there a way to save the socket info on disconnect then if the same socket tries to connect have it recognize the user and continue? </p>
  
  <p><strong>Additonal:</strong> I am thinking maybe I need to focus on creating a user login with mongodb first, then using that log in session data and pass that to the socket, creating a socket object with current database details? Does that sound like something that makes more sense, is that possible?</p>