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');
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.
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.
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.
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?
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
https://github.com/socketio/socket.io/commit/b73d9bea4efb48277eee685763026ff2df5a79ab
break these thins
PR merged.