0

I want to know the room in Which the socket is in ? I want to broadcast to the other sockets in the room during the disconnect event

roh_dev
  • 275
  • 2
  • 6
  • 17
  • I find it easiest to keep track of socket references myself, in a object or Map or something. For example: https://stackoverflow.com/questions/11356001/socket-io-private-message/11356019#11356019 – Brad Apr 17 '18 at 01:13
  • Actually i am using another way now. I create an object which will store socket id as the key and room id as the value corresponding to the socket id. So when i need the room id i fetch it from the object – roh_dev Apr 17 '18 at 01:33
  • If you're only ever going to have a socket in one room at a time, you can just set that room as a property on the `socket` object when you join it to the room. `socket.currentRoom = room`. Then, upon disconnect, you can just access `socket.currentRoom` to see what room you had it in. – jfriend00 Apr 17 '18 at 01:59
  • @jfriend00 yea this is a better method in comparison to my method of creating an object and setting its property/value . Thanks a lot! – roh_dev Apr 17 '18 at 02:15

1 Answers1

2

If you're only ever going to have a socket in one room at a time, you can just set that room as a property on the socket object when you join it to the room. socket.currentRoom = room. Then, upon disconnect, you can just access socket.currentRoom to see what room you had it in.

It is possible to dive into internal data structures to find out what rooms a socket is in, but if you're just using one room, then the socket.currentRoom is probably easiest.

jfriend00
  • 683,504
  • 96
  • 985
  • 979