Skip to content

Default room name is not socket id #2475

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
tw0517tw opened this issue Mar 8, 2016 · 6 comments
Closed

Default room name is not socket id #2475

tw0517tw opened this issue Mar 8, 2016 · 6 comments

Comments

@tw0517tw
Copy link
Contributor

tw0517tw commented Mar 8, 2016

I'm using socket.io 1.4.5.

if I do

io.to(socketId).emit('some msg');

the message will not be emitted.

after digging a little bit I found that I should prepend /# before socke id.

socketRoomName = '/#' + socketId;
io.to(socketRoomName).emit('some msg');
@baconbrad
Copy link

A socket ID is not the same as a room ID. The socket ID is unique per client. So the above is sending 'some msg' to that specific client and not a room.

Think of a room as a group of people and a socket as a singular person in this context.

@baconbrad
Copy link

Also io.emit('some msg'); will send it to everyone in the default room. So if you have no use for multiple rooms you can omit it's usage completely from your code and let socket.io handle it.

Take note 'some msg' is the event. So you might be looking to do something like io.emit('msg', 'some msg'); instead and listen for msg on the client side.

@tw0517tw
Copy link
Contributor Author

tw0517tw commented Mar 9, 2016

But the document says that every socket joins a room of its socket id.
http://socket.io/docs/rooms-and-namespaces/#default-room

I think this is related to #2405 , that client and server returns different format of socket id.

@ghost
Copy link

ghost commented Mar 11, 2016

You're right. Just ignore the guy above.

I lost two hours trying to figure out what was going on. Turns out when I did THISID = socket.id it got stored as "EgdohcmzQZnc_FzHAAAA" instead of ('/#' + "EgdohcmzQZnc_FzHAAAA") which is the true socket id. So server side socket.id is /#EgdohcmzQZnc_FzHAAAA, but client side socket.id is EgdohcmzQZnc_FzHAAAA. Who is responsible for this?

@tw0517tw
Copy link
Contributor Author

tw0517tw commented Apr 1, 2016

Dig a little bit in the source I found that it's this line
https://github.com/socketio/socket.io/blob/master/lib/socket.js#L62
form this commit
b73d9be
break these thins

@tw0517tw
Copy link
Contributor Author

PR merged.

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

No branches or pull requests

2 participants