How to get a list of online users except the current user who is viewing it in Node.js

Problem

I am using socket.io to make a chat application where I need to show the current user list. Till now it is working fine with the current user list being displayed

I am using this code for maintaining the user list

usernames[socket.id] = name;//where name is the name of the user

Then on disconnect I am again deleting that user from the list

delete usernames[socket.id];

But, my problem with this method is that suppose there are three users a,b and c

now the userlist will be populated on the connection event,something like this

 io.sockets.on('connection', function(socket) { 

    ...

    socket.on('register', function(name) {

    usernames[socket.id] = name;

And the user list will be something like "a,b and c"

But, what I want is that for user "a" the list should show "b and c" and for "b" it should be "a and c" and for "c" it should be "a and b".

I also thought about removing it on the client side based on some variable which stores the name of the user who is viewing it but for this chat multiple users can have the same name. So if suppose all the three have name "a" then it will remove all three instead of removing only one name from the list.

....I guess I am out of ideas on this one. Plz help me to implement it in socket.io

Problem courtesy of: Nav

Solution

Your question has nothing to do with Socket.IO really. When displaying a list of users, simply don't display the current user!

for (userIndex in users) {
    if (users[userIndex].id !== currentUser.id) {
        // Add user to displayed user list
    }
}
Solution courtesy of: Brad

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.