sarabs3
sarabs3

Reputation: 594

Does socketio creates a room always when a socket gets coonected?

I am creating a private chat app want to add specif sockets on rooms based on their interest and behaviours and i want an user to be in a room at any time.

My question here is does socket.io automatically create a room whenever a new socket gets connected or it happens because of my code?

var allSockets = []; // Adding all new sockets in an array

console.log(allSockets.length); // checking length of an array after socket connection

app.get('/api/rooms',function(req,res){
  res.send(io.sockets.adapter.rooms); // just a url to see the rooms connected
})

Above is an example of code and below is an example of output when two users gets connected

{"/#DtUmoNBKeQSYMcepAAAA":{"sockets":{"/#DtUmoNBKeQSYMcepAAAA":true},"length":1},"/#1FXDSOViOj2R64DjAAAB":{"sockets":{"/#1FXDSOViOj2R64DjAAAB":true},"length":1},"57b2ba38426f18d1040a6d5f":{"sockets":{"/#1FXDSOViOj2R64DjAAAB":true,"/#DtUmoNBKeQSYMcepAAAA":true},"length":2}}

I can see in the above result that two users have their own room as well a new room created by where they both are together. So total of three rooms.

Is there any way to get rid of default rooms socketio creates if any.

Upvotes: 0

Views: 307

Answers (1)

jfriend00
jfriend00

Reputation: 707466

My question here is does socket.io automatically create a room whenever a new socket gets connected or it happens because of my code?

Yes, a room is automatically created based on each newly connected socket's id value and that socket is joined into that room. No other sockets are put in that room. This is just something that socket.io does as part of it's own housekeeping. It is not because of your code.

You can, for the most part, ignore that room. It has a unique prefix if you're trying to filter it out of some enumeration.

Is there any way to get rid of default rooms socketio creates if any.

Not that I know of. And, doing so might mess up some socket.io internals.

Upvotes: 1

Related Questions