简体   繁体   中英

Socket.io: Check if client already joined a room before adding

I am writing a simple chat application with chat rooms, and users can be in one or more chat rooms and users in a room can broadcast messages to all users in that room. I am using PHP with socket.io and node.js to implement push notifications.

The question might sound ridiculous given my lack of exposure to socket.io but couldn't find any answer to this. I might have gotten the fundamentals about sockets wrong, but is it right to check if a client is in a room or not, before adding a client?

And suppose if the user refreshes a page, should the client be added back into the room again, or should the client list in a given room be persisted somewhere so that whenever there is a page refresh, they should be added back into the room again? Wouldn't this also reduce the number of socket calls to the server from the clients?

Unsure about the best practices around this, so thought of asking here, please advise.

This is sample from github thread:

io.on('connection', function(socket){
  socket.join('room1');
  console.log(socket.rooms); // contains an object with all of the roomnames as keys and values
})

You can also do:

io.sockets.adapter.rooms['myRoomId']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM