0

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

Nav
  • 10,304
  • 20
  • 56
  • 83

1 Answers1

1

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
    }
}
Brad
  • 159,648
  • 54
  • 349
  • 530
  • Actually I thought socket.io had some magical method in its arsenal...thanks for the reply though...this code is for the front end right...it gets confusing sometimes with the same code on both server and front end...:( – Nav Nov 24 '12 at 20:05
  • Yes, you would use something like this on the front-end. There is no magic, and nor does this problem require magic. Socket.IO is your communications layer, nothing more. While it is convenient for chat applications and what not, it should not be assumed that that's all it can be used for. It's methods are not catered for one particular purpose, and it would be bad form if it had logic to do what you are looking for anyway. This stuff is better left up to your application, for flexibility. – Brad Nov 24 '12 at 20:06
  • hey how can i access socket.id on the front end??? it thought it can be accessed only on the server end...If i am thinking correctly u meant to refer currentuser.id as socket.id right? – Nav Nov 24 '12 at 20:10
  • @Nav, No, I am referring to the ID of the user. You do keep track of some unique identifier for users, right? While it is possible to get the connection ID, you shouldn't use it. – Brad Nov 24 '12 at 20:12
  • nope there is no unique id for the user ...this chat is for users who want to just chat with technicians regarding some inquiries they wanna make regarding my website and they are random users who only chat for 1 or 2 minutes so there was no point in storing user id for us..they just inquire and go... – Nav Nov 24 '12 at 20:16