var socketIo = require('socket.io');

var weIo = {};

/**
 * Add socket.io in http
 */
weIo.load = function load(we, server) {
  we.io = socketIo(server);

  we.events.emit('we:after:load:socket.io', { we: we, server: server } );

  // socket.io auth middleware
  we.io.use(function (socket, next) {
    if (socket.handshake && socket.handshake.query && socket.handshake.query.authToken) {
      we.auth.tokenStrategy.bind({we: we})(socket.handshake.query.authToken, function(err, user) {
        if (err) return next(err);
        if (!user) return next();

        socket.authToken = socket.handshake.query.authToken;
        socket.user = user;

        next();
      });
    } else {
      next();
    }
  });

  we.io.on('connection', function (socket) {
    we.log.verbose('a user connected:', socket.id);

    if (socket.user && socket.user.id) {
      // join user exclusive room to allow others users send
      // mesages to this user
      socket.join('user_' + socket.user.id);
    }
    // global socket to system calls
    socket.join('global');
    // Public room
    socket.join('public');

    socket.on('auth:login:token', function(data) {
      if (!data.authToken) return;
      we.log.verbose('auth:login:token', data);

      we.auth.tokenStrategy.bind({we: we})(data.authToken, function(err, user) {
        if (err) {
          return we.log.error('auth:login:token: we.auth.tokenStrategy:', err);
        }
        if (!user) return;

        socket.authToken = data.authToken;
        socket.user = user;
        socket.send('auth:authenticated', {
          user: user,
          token: socket.authToken
        });
      });
    });

    socket.on('disconnect', function() {
      we.log.verbose('user disconnected', socket.id, socket.user);
    });
  });
}

module.exports = weIo;
@@ -29,16 +29,43 @@
 et.io');
+%0Avar _ = require('lodash');
 %0A%0Avar we
@@ -251,16 +251,43 @@
 r %7D );%0A%0A
+  we.io.onlineusers = %7B%7D;%0A%0A
   // soc
@@ -1825,17 +1825,1196 @@
     
-%7D);%0A  %7D);
+  we.io.removeFromOnlineUsers(socket);%0A    %7D);%0A  %7D);%0A%0A  we.io.removeFromOnlineUsers = function removeFromOnlineUsers(socket) %7B%0A    if (typeof we.io.onlineusers%5Bsocket.user.id%5D === 'undefined') return;%0A    if (typeof we.io.onlineusers%5Bsocket.user.id%5D === 'undefined') return;%0A    if (!socket.user %7C%7C !socket.user.id) return;%0A%0A    if (we.io.onlineusers%5Bsocket.user.id%5D) %7B%0A      var index = we.io.onlineusers%5Bsocket.user.id%5D.sockets.indexOf(socket.id);%0A      if (index %3E-1 ) we.io.onlineusers%5Bsocket.user.id%5D.sockets.splice(index, 1);%0A%0A      if (!we.io.onlineusers%5Bsocket.user.id%5D.sockets.length)%0A        delete we.io.onlineusers%5Bsocket.user.id%5D;%0A    %7D%0A%0A    if (!we.io.isOnline(socket.user.id) ) %7B%0A      we.db.models.contact.findUserContacts(socket.user.id)%0A      .then(function (contacts) %7B%0A        contacts.forEach(function(c) %7B%0A          var cId = c.to;%0A          if (c.to == socket.user.id) cId = c.from;%0A          we.io.sockets.to('user_' + cId).emit('contact:disconnect', %7B id: socket.user.id %7D);%0A        %7D)%0A      %7D);%0A    %7D%0A  %7D%0A%0A  we.io.isOnline = function isOnline(userId) %7B%0A    if (_.isEmpty(we.io.onlineusers%5BuserId%5D)) %7B%0A      return false;%0A    %7D else %7B%0A      return true;%0A    %7D%0A  %7D
 %0A%7D%0A%0A

add logic to remove user from socket.io online users object