mishchief
mishchief

Reputation: 541

Socket io creates rooms when a socket connects

Every time a socket joins a room is created in my application. Is this normal behavior?enter image description here

I am not creating these rooms, as you can tell the room name is called the ID of the socket, which I find weird. Is this normal behavior?

Upvotes: 0

Views: 257

Answers (1)

jfriend00
jfriend00

Reputation: 707456

This is normal behavior. Socket.io creates a room with the name of the connection's socket.id and automatically places only that socket in the room. This allows you to do things like:

io.to(someSocketId).emit(...)

because the socket.id is also a room name. Since socket.id values are unique and random and because all room participation is controlled by the server only, it's really just a server housekeeping thing that doesn't affect anything else.

If you have some reason to want to be able to discern which rooms are ones you created and which ones are the automatic socket.id form, you can put a unique prefix on the ones that you create such as an underscore and then you can tell which ones are yours and which ones are created by the system.

When the connection disconnects, that room will also go away.

Upvotes: 2

Related Questions