Skip to content

Mechanism to add a client to a room with only his ID? #1851

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

Closed
alexcastano opened this issue Nov 2, 2014 · 10 comments
Closed

Mechanism to add a client to a room with only his ID? #1851

alexcastano opened this issue Nov 2, 2014 · 10 comments
Labels
enhancement New feature or request

Comments

@alexcastano
Copy link

I see currently it is possible to send a message to a socket because a default room is created with his ID.

However I don't know if it would be possible to add this socket to a room, to remove from a room or just to disconnect it, of course in multi server environment. Any ideas?

Thank you!

@peteruithoven
Copy link

Wait, what are you asking here?

  • You can add sockets to rooms
  • You can remove sockets from rooms
  • In multi server environments you can broadcast to all clients in rooms using socket.io-redis, but you can't (yet) list them.

@alexcastano
Copy link
Author

I mean, in a multiserver environment, given a socket id string, I can send a message using:

io.to(string_id).emit('hello')

But I cannot do:

io.join(string_id, room)
io.disconnect_socket(string_id)

Because the socket could be in the current server or maybe in other one.

I hope I've explained better.

@peteruithoven
Copy link

What I would do is setup a redis pub/sub channel. Make sure all connected sockets (on all servers) listen to this channel. Then send these kind of commands over this channel.
I wrote the following library to make this slightly easier.
https://www.npmjs.org/package/redis-pubsubber

@alexcastano
Copy link
Author

Hello. Thank you for you answer. I see, I could implement it, however I feel like this feature should be implemented by socket.io. Moreover when in the next version you can list the client of a room: #1630

So this is more a feature request if there is not a simple way to do it, a good api to manage clients easily from any server. What do you think?

@rauchg
Copy link
Contributor

rauchg commented Nov 3, 2014

Need to think about whether we'll add this feature. I can definitely see the usecase.

@Tug
Copy link

Tug commented Nov 5, 2014

I think this could be really handy for implementations where you have a lot of publishers and a few subscribers. In that case most rooms are empty so it's a waste to broadcast to all nodes.
I had a cool optimization in 0.9 where I would do:

if(io.of(nsp).clients(room_id).length > 0) {
    io.of(nsp).in(room_id).emit('m', message);
}

But now that a single node does not see the whole list of clients it cannot be done without some pubsub mechanism to make the publisher join the room on command.

@nkzawa nkzawa added enhancement New feature or request Rooms labels Dec 25, 2014
@hekike
Copy link

hekike commented Jun 27, 2015

Did you find the right solution how to do this in multi-server env?

I really liked @alexcastano 's approach: io.join(socketId, roomId)

@infnada
Copy link

infnada commented Feb 25, 2016

Maybe this can help you #2458

@darrachequesne
Copy link
Member

Closed by socketio/socket.io-redis-adapter#168.

io.adapter.remoteJoin('<my-id>', 'room1', function (err) {
  if (err) { /* unknown id */ }
  // success
});

io.adapter.remoteLeave('<my-id>', 'room1', function (err) {
  if (err) { /* unknown id */ }
  // success
});

@Assdi
Copy link

Assdi commented Nov 20, 2017

can i do this without redis ? @darrachequesne

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants