W
W
w_b_x2018-03-01 01:43:51
JavaScript
w_b_x, 2018-03-01 01:43:51

How to capture user disconnect from Socket.io room?

Hello.

There is a problem:
A bunch of NodeJS and Socket.IO is used. There are two pages that connect to the socket server. One simply connects, and the second connects and subscribes to room events. The problem is that I want to process the exit from these two pages differently, if the tab/disconnect is closed on a normal page, then do nothing, and if on the page with a subscription to the room, then execute the code (ala set the offline status to user database).

How is it possible to understand that a person has just disconnected from the room , and not just left?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2018-03-01
@w_b_x

You drive the client into a room on the server with the join command, create an object into which you will shove the ids of the connected sockets and, if necessary, the ids of the rooms where you brought him. When the disconnect event is triggered, check this object, make entries in the database and delete obsolete data from it.
When a client socket is disconnected from the server, it is automatically thrown out of all rooms, so most likely you will no longer receive information in which rooms it was at the time of disconnection, although here you can not guess but look at the socket objects in debug, find where they store information about condition of the rooms and draw appropriate conclusions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question