3

Hey I was looking at socket.io and I was wondering if the socket.id persists even after a disconnect ? I am thinking it does not does anyone have any prior experience with disconnects and session management?

I was thinking of using the socket.id to be the primary key to access the user session state. Though I am worried about it not being associated with the user on a disconnect leaving a orphaned user on the server side.

George Host
  • 980
  • 1
  • 12
  • 26

1 Answers1

0

Socket.io emits disconnect event after the disconnection. So when the disconnect event is triggered you can clear the session of the user.

Server

io.sockets.on('connection', function(socket) {

   socket.on('disconnect', function(data) {
      console.log('socket '+ socket.id + 'disconnected!');

      //delete the users socket here with socket.id
   });
});

This answer(https://stackoverflow.com/a/17311682/453486) helps for connection and disconnection of users

Community
  • 1
  • 1
Srikanth Jeeva
  • 3,005
  • 4
  • 38
  • 58