title: How to check a user online or not in socket io and android chat application
body: <p>I am using socket io and android for using chat application. The server side is nodejs and client side is android. My chat is working as fine but i want to check whether a user is active or not. For example i want to check wheather user "abc" is active or not. </p>
  
  <p>In Android part. I have get the reference.</p>
  
  <pre><code>emit("login",userid);
  </code></pre>
  
  <p>is used for get online users. So i use</p>
  
  <pre><code>public class OnlineActivity extends AppCompatActivity {
  private Socket mSocket;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_online);
  
      ChatApplication app = (ChatApplication) getApplication();
      mSocket = app.getSocket();
  
      JSONObject user = new JSONObject();
      try {
          user.put("userId", "abc");
  
  
  
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      mSocket.emit("login",user);
  
      Log.d("LOG", String.valueOf(mSocket.emit("login",user)));
      Log.d("LOG", String.valueOf(user));
  }
  }
  </code></pre>
  
  <p>The server code is shown below </p>
  
  <pre><code>var express = require('express');
  var app = express();
  var server = require('http').Server(app);
  var io = require('socket.io')(server);
  var port = process.env.PORT || 86; 
  server.listen(port, function () {
      console.log('Updated : Server listening at port %d', port);
  });
  app.use('/js',  express.static(__dirname + '/public/js'));
  app.use('/css', express.static(__dirname + '/public/css'));
  app.use(express.static(__dirname + '/public'));
  var usernames = {};
  var numUsers = 0; 
  io.on('connection', function (socket) {
  var addedUser = false;
  socket.on('new message', function (data) {
    socket.broadcast.emit('new message', {
      username: socket.username,
      message: data,
      timestamp: Date.now()
    });
    console.log('I sent it');
  });
  
  // when the client emits 'add user', this listens and executes
  socket.on('add user', function (username) {
    // we store the username in the socket session for this client
    socket.username = username;
    // add the client's username to the global list
    usernames[username] = username;
    ++numUsers;
    addedUser = true;
    socket.emit('login', {
      numUsers: numUsers
    });
    // echo globally (all clients) that a person has connected
    socket.broadcast.emit('user joined', {
      username: socket.username,
      numUsers: numUsers
    });
  });
  
  // when the client emits 'typing', we broadcast it to others
  socket.on('typing', function () {
    socket.broadcast.emit('typing', {
      username: socket.username
    });
  });
  
  // when the client emits 'stop typing', we broadcast it to others
  socket.on('stop typing', function () {
    socket.broadcast.emit('stop typing', {
      username: socket.username
    });
  });
  
  // when the user disconnects.. perform this
  socket.on('disconnect', function () {
    // remove the username from global usernames list
    if (addedUser) {
      delete usernames[socket.username];
      --numUsers;
  
      // echo globally that this client has left
      socket.broadcast.emit('user left', {
        username: socket.username,
        numUsers: numUsers
      });
    }
  });
  });
  </code></pre>
  
  <p>How it possible please help me?</p>