Detecting user disconnection?

Is there any way to detect when a user connects or disconnects from a Dash app? I’m using a server-side Redis caching (to avoid serialization, as several hundreds mb would be exchanged in some callbacks), but I can’t afford more than a few users loading data at a time because of RAM limitations. In practice, each user will a priori only load a small (but different) part of the main dataset my app allows to explore. I would like to know when I can free the memory of the Redis cache with no risk, i.e when the user disconnects.

I’m looking for a way of detecting any kind of disconnection: new URL has been entered, browser got closed, connection lost, etc. I believe I can do that with websockets, but I didn’t even know this was a thing until today, and I couldn’t find a tutorial on how to handle user connection/disconnection for a dash app. I thought session id could help, but they do not tell me when the user gets disconnected. Is there any simple way of solving my problem?

Note that although my data is read-only, I can’t work with one large global variable for the dataset as it is huge and doesn’t fit in RAM memory. I’d like to avoid working with the filesystem (hdd) as it would be too slow.

I don’t think there is any easy way to do it. Unless you use a websocket, you will probably have to keep track yourself using some kind of time out mechanism.

Thanks a lot :slight_smile: I’m actually open to any solution, but was just curious if there was a one-liner or very simple approach that could do the job. Well, if you have any suggestion with time-out, I’m happy to take it. Otherwise, I will investigate websockets more.

The time out solution would be to add an Interval component on the page that signals the server in a callback that “I am still alive!” every say 5 seconds. Then, if the server has not received an “I am still alive!” message after say 15 seconds, it will delete the cache. Adjust the time intervals as you see fit.

1 Like

Another solution that I’ve used in the past is to use Redis’s built-in expiry, that way the Python program doesn’t need to handle this logic itself.

I would delete keys after 48hr, and include error handling logic in the app if it tried to access a key that didn’t exist. Most sessions are short, but if someone left their browser open for a couple of days then at least they would get an error message.

1 Like