question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default room name is not socket id

See original GitHub issue

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');

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
baconbradcommented, Mar 8, 2016

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.

1reaction
ghostcommented, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

socket.io emit to default room does not work - Stack Overflow
It was discovered that the default room socket.id being used by the server to set the default room is not the same as...
Read more >
Rooms | Socket.IO
Default room ​. Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id. For your convenience, each socket ...
Read more >
How to manage users in socket.io in Node.js ? - GeeksforGeeks
getUser: This function takes id as a parameter and returns the username by finding it from the array. getUsersInRoom: This function will return ......
Read more >
Handling SocketIO rooms with React Hooks - Level Up Coding
Each socket can join required socket.io room by emitting room name to join event, and the server would listen to join event and...
Read more >
Socket.io Namespaces and Rooms - DEV Community ‍ ‍
By default, if a namespace is not specified, they will be attached to the default namespace / . Namespaces are used to separate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found