40

I've made a chat client with different chat rooms in NodeJS, socketIO and Express. I am trying to display an updated list over connected users for each room.

Is there a way to connect a username to an object so I could see all the usernames when I do:

var users = io.sockets.clients('room')

and then do something like this:

users[0].username

In what other ways can I do this?

Solved:
This is sort of a duplicate, but the solution is not written out very clearly anywhere so I'd thought I write it down here. This is the solution of the post by Andy Hin which was answered by mak. And also the comments in this post.

Just to make things a bit clearer. If you want to store anything on a socket object you can do this:

socket.set('nickname', 'Guest');    

sockets also has a get method, so if you want all of the users do:

for (var socketId in io.sockets.sockets) {
    io.sockets.sockets[socketId].get('nickname', function(err, nickname) {
        console.log(nickname);
    });
}

As alessioalex pointed out, the API might change and it is safer to keep track of user by yourself. You can do so this by using the socket id on disconnect.

io.sockets.on('connection', function (socket) { 
    socket.on('disconnect', function() { 
        console.log(socket.id + ' disconnected');
        //remove user from db
    }
});
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
kimpettersen
  • 1,602
  • 2
  • 13
  • 18
  • possible duplicate of [Socket.IO - how do I get a list of connected sockets/clients?](http://stackoverflow.com/questions/6563885/socket-io-how-do-i-get-a-list-of-connected-sockets-clients) – alessioalex Jan 09 '12 at 13:12
  • 2
    how do you do this in socket.io v 1.0 above – SamFast May 02 '15 at 11:45
  • 1
    @SojharoMangi For v 1.0+ https://github.com/Automattic/socket.io/blob/master/examples/chat/index.js#L35-L36 Or http://stackoverflow.com/a/24393900/2609085 – Gaurav Gupta May 13 '15 at 11:59
  • @SojharoMangi This might also help you http://stackoverflow.com/questions/24154480/how-to-update-socket-object-for-all-clients-in-room-socket-io/25028902#25028902 – Sony Mathew Oct 06 '15 at 02:00

2 Answers2

48

There are similar questions that will help you with this:

Socket.IO - how do I get a list of connected sockets/clients?

Create a list of Connected Clients using socket.io

My advice is to keep track yourself of the list of connected clients, because you never know when the internal API of Socket.IO may change. So on each connect add the client to an array (or to the database) and on each disconnect remove him.

Community
  • 1
  • 1
alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • I've tried to keep track myself, but how do I know which user disconnects? I've tried to make a function that pings all the clients when someone disconnects, but that was not a very reliable solution. – kimpettersen Jan 09 '12 at 13:37
  • 8
    io.sockets.on('connection', function (socket) { socket.on('disconnect', function() { console.log(socket.id + ' disconnected'; }); <= so through socket.id, that's the only thing unique to the connection – alessioalex Jan 09 '12 at 13:39
  • Glad to help, by the way you can accept & upvote my answer if it was useful to you. – alessioalex Jan 09 '12 at 13:58
  • I know, but I don't have 15 in reputaion yet, so I can't. I made a solution edit and mentioned your username. I'll go back and upvote when I've reached the limit. – kimpettersen Jan 09 '12 at 14:22
1

In socket.io@2.3.0 you can use:

Object.keys(io.sockets).forEach((socketId) => {
  const socket = io.sockets[socketId];
})
Alen Vlahovljak
  • 542
  • 3
  • 16