Jump to content

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

From Wikipedia, the free encyclopedia
Computing desk
< November 1 << Oct | November | Dec >> November 3 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


November 2

[edit]

Closing application session on browser close

[edit]

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. JIP | Talk 22:19, 2 November 2020 (UTC)[reply]

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)[reply]

  • 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)[reply]
    • 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. JIP | Talk 22:56, 2 November 2020 (UTC)[reply]
      JIP, 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)[reply]
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)[reply]

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)[reply]