Wikipedia:Reference desk/Archives/Computing/2020 November 2

= November 2 =

Closing application session on browser close
I am developing a web application at work. One of the requirements I've come across is that the application should close the user's session when the user's browser is closed. I am not sure this is actually possible. There is a JavaScript event "onunload" or "onbeforeunload" which is apparently fired when the user navigates away from the page. But what if the user's browser crashes, or the user simply powers their computer down? This way the event doesn't get the chance to fire. And besides, the event fires when the user navigates away from the current page - it doesn't distinguish between moving to another page in the same application or navigating away from the application altogether, making it useless even if it fires. And as the application is access-controlled anyway as the user can only access its pages if there is a valid session token, is this even necessary? It could be thought of as a feature that the user's session continues even if their browser accidentally crashes in the middle. J I P &#124; Talk 22:19, 2 November 2020 (UTC)

You may be able to detect when the browser is closed or otherwise disconnected by writing a client side script to contact the server every few seconds. Answering the rest of your question may be aided by further description of your application, if possible. Wildjerry (talk) 22:42, 2 November 2020 (UTC)
 * It depends a lot on the OS, environment, what sort of session etc. but could your application first launch the browser? That way it will own the task that is running the browser and know when it closes — GhostInTheMachine talk to me 22:50, 2 November 2020 (UTC)
 * No, it can't, as it's a web application in the first place, just like Wikipedia or Facebook or what have you. The user only interacts with it through their web browser. I only use the term "application" because it is developed with an ASP.NET MVC code-behind instead of being completely client-side. From the point of view of the user's computer, it's a web site just like any other. J I P  &#124; Talk 22:56, 2 November 2020 (UTC)
 * , the best you're going to do is by implementing a timeout, best paired with a keepalive ping or similar, that queries the client to see if it is still around. Elizium23 (talk) 01:39, 3 November 2020 (UTC)
 * Consider using WebRTC - TCP connection from browser to your server. Server will detect when connection drops (page closed) and then can close the session. But the server may not detect instantaneously, and implementing this WebRTC feature only to kill session appears to be overkill. manya (talk) 09:46, 3 November 2020 (UTC)

The TCP connection will eventually close and you can detect that, but it can take up to 20 minutes, since the timeouts were chosen in an era where a failed connection often meant some cable had gotten unplugged, so you'd want to wait long enough for the person to plug it back in. The only way to get reasonably quick knowledge that the client has disconnected is to have the client send a heartbeat packet, say 1x a second or so. 2602:24A:DE47:BB20:50DE:F402:42A6:A17D (talk) 23:45, 9 November 2020 (UTC)