-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Ability to get all clients for a given room #1428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I'm new and I don't want to mess, but this function seems to already exist: |
+1 for @Ijatsu - just for completeness, here's how you do it word for word from the wiki: "If you want a list of clients in a particular room, call io.sockets.clients('room'). This will return Socket instances of all clients in the room." Is there anything this method doesn't do that you need @crickeys? |
I'll merge this. Just need code style fixes and tests |
@crickeys I noticed yesterday that the Adapter was ripped out of socket.io core and put into its own repo at https://github.com/LearnBoost/socket.io-adapter - you may need to update your PR accordingly |
Any updates on merging this? Much needed. :-) |
What's your usecase specifically @gtramontina ? I'm curious because I've always been fine with |
I'm rewriting a collaborative whiteboard I've written a few years ago. socket.on('connection', join);
function join (socket) {
// ...
var room = socket.request._query.room; // the underscore here was already a bit tricky :-)
var lastParticipant = this.sockets.clients(room).last(); // .last() here just for the sake of brevity
lastParticipant.emit('get snapshot');
lastParticipant.on('snapshot', function (canvas) { ... });
// ...
} Do you see a better way of achieving this, or even a completely different approach? I'm trying not to have much state on the server side... |
The fact this doesn't seem necessary is puzzling. Is there a better way to list the connected users in a room ? Or to emit a message to only some sockets in a room ? |
Was looking to drop in v1.0 into NodeBB, although we use We're also referencing |
+1 |
Here's my use case. I have a calling system which should only send call message to one tab of one user. Each socket is joining a room named like the user id. In 9.x i was using redis (to be able to work with multiple worker processes) and i was calling io.sockets.clients(userId), which seemed to be correctly returning all sockets connected to the room named like the user id, and i was just picking the last socket and sending the message there. |
Also, the ability to manually disconnect specific clients in a room from the server side was useful. |
This is really frustrating that there is no way to get a list of all connected users. Previously we did it by |
It looks like all developpers have made a similar workaround function. It's just sad this common function isn't standard, especially as it was before. |
@ismarslomic Thanks, that is helpful information, but it did not solve my issue. I do not use specific rooms, just 'default' one. And from this post I understand that default room is ''. var clients = io.sockets.adapter.rooms[''];
console.log(clients); I get 'undefined' |
Ok. So what you really want know is number of connected socket to server: |
My use case is similar to @julianlam. I’d like to update a list of connected room-users in real-time. |
Well, if you are not joining particular room then counting connected clients at server can been done be getting length of socket array like I described above. Does this solve your use case? |
@tscheurer Is that what you want : http://stackoverflow.com/q/24059287/263525 ? |
@ismarslomic Thanks, but I just don't see how the number of connected sockets will help me identify specific socket. |
I’m currently creating an old-fashion room-based chat. Users can just enter a nickname and join a room. I’m going to store the nickname on the socket. If a client requests the list of the users in the current room, I’m going to take the list from io.sockets.clients(roomName)and iterate through it, in order to send all the nicknames to the requesting client. A possibility would be to handle the users in a room by myself, but until I moved to 1.0 it was working fine with io.sockets.clients(roomName). @Canop: so far i was doing it as suggested in the answer on stackoverflow. |
Pretty strange behavior: This however: Iterating over the array using |
So if I broadcast to a room with the redis adapter does it get sent to connected sockets on all nodes? Or is that not implemented as well? |
@pravincar if you're using |
The main reason I didn't want local-only |
Are you fine with clients giving you guys the array of ids? It won't give you |
I see :) @guille Totally fine with the array of ids.. as long as you're giving it to me tonight, I'll bow down to you :P Demo tomorrow and my app can't work if I don't know who's connected |
If you need a quick workaround for now you can always query redis. We'll get this out asap, but today we're making a patch release first with a few little but important bugfixes. Then this is going to be in the next release |
Cool. Will do that for now. Thanks! On Fri, Jun 13, 2014 at 11:18 PM, Guillermo Rauch [email protected]
|
What is the key in redis where I can find this? Am unable to locate this. |
Hum... given the ID, is there a way to target that specific connection and send it a message? Something like: find the socket from an ID and |
@gtramontina Every connection is automatically assigned to a room which is identified by its socket id. So you can do |
then perfect! :-) |
@guille Client ids seem like an ok solution for the given use cases. |
I wrote the code for the clients function to retreive the ids of sockets in a given room. |
Didn't this PR already have commits? |
My PRs makes it async so it can also work with redis, across nodes |
Looks good to me. Will test it out and give you feedback. |
@pravincar I am of the opinion that it should be as lean as possible. It's trivial to do an async.map to get the meta data you need after retrieving the ids |
@FREEZX I commented on your PR with regards to the signature |
We'll follow the discussion in @FREEZX's PR |
@pravincar Something like the old client.get and .set methods would make sense, but given the fact that you cannot directly access sockets of other nodes, i don't think that would be useful at the moment. Now you could keep your own data structures associated with the socket id. |
👍 |
…will not work in multi-node environment. Fix: http://stackoverflow.com/questions/23858604/how-to-get-rooms-clients-list-in-socket-io-1-0 Request: socketio/socket.io#1428 Pull requests: - socketio/socket.io#1630 - socketio/socket.io-adapter#5 - socketio/socket.io-redis-adapter#15
👍 |
Why has this not been implemented yet? |
Not sure if I fully understand all the parts to the new socket 1.0, but I needed this ability. I believe I separated the parts into the namespace and adapter correctly, but please do check me over. Thanks!