Considering a multi-chat application.
Users can join multiple rooms ( socket.join(room) ), users can leave a room ( socket.leave(room) ).
When socket is leaving a room I notify the other room participants. If the socket is currently in 3 rooms, and he suddenly disconnects from the website without leaving the rooms the proper way, how can I notify those rooms that the user has left ?
If I work with the on socket disconnect event, the user will no longer be in any room at that point. Is the only way keeping a separate array of users, or is there some clever way I haven’t thought about?
During the disconnect event the socket is still available to your process. For example, this should work
Although this is not actually necessary as socket.io will automatically prune rooms upon a disconnect event. However this method could be used if you were looking to perform a specific action.