Detect Browser close event in server side.

Hi,
I need to destroy vaadin session when browser/tab close. That means when a user will close the browser than server side will be response for something. So how I can do in vaadin flow.

Regards
Bishwajit Barua

Actually in most cases you should not have need for this, however if you have some special configuration, there may be real need. You can find more information from here: https://vaadin.com/docs/v12/flow/advanced/tutorial-application-lifecycle.html

Application Lifecycle (at https://vaadin.com/docs/v12/flow/advanced/tutorial-application-lifecycle.html) doesn’t contain any clue regarding identifying browser or tab closed event!!
This is a very important event and personally I’m quite surprised how Vaadin doesn’t provide developers with such interface to handle and manage such situation.
On a contrary with @Tatu_Lund’s comment, I believe there are dozens of scenarios in which this feature is required. for example, lets say I have a synchronous connection to a message broker server like RabbitMQ and I declare and open queues based on customer’s visit to my application. I don’t want to keep those connections and queues to be alive even after the user has left the app (closed the browser or tab) and I want to remove the mentioned queues and close the connection to my broker.
How shall I do it, when I can’t identify user’s exit event?!!

Thats logically not really possible. You can do some kind of hackish solution that works some times, but just imagine
the user just closing the browser process via the OS … there is nothing active anymore that would be able to send this event to the server…

So usually one deals with this using timeouts ( e.g inactivity ) for cleanup.
For broker connections you could pool them and only use them when a user needs them and not tie their lifetime to the browser session at all.

doesn’t contain any clue regarding identifying browser or tab closed event!!

There is no event with that name. Normally you should use e.g. onDetach on mainlayout or UI::addDetachListener(listener), which should happen eventually when enough heart beats are missed and UI is wiped. Vaadin’s client part sends heart beats on regular interval, and after server misses three heart beats it concludes Browser lost and UI is marked to be wiped. The last UI is wiped after session time out provided that closeIdleSessions parameter is set to true (see also: https://vaadin.com/docs/v14/flow/advanced/tutorial-application-lifecycle.html#application.lifecycle.ui-expiration ).

If Browser is closed nicely, it can be detected, but there is lot of edge scenearios why this not reliable, however the method is decribed here

https://stackoverflow.com/questions/60084612/java-vaadin-14-detect-user-leave-closes-tab-f5-etc/60088823#60088823

So you should not rely on this alone, you need to observe UI detach as a backup anyway.

Bernhard Glück:
Thats logically not really possible. You can do some kind of hackish solution that works some times, but just imagine
the user just closing the browser process via the OS … there is nothing active anymore that would be able to send this event to the server…

So usually one deals with this using timeouts ( e.g inactivity ) for cleanup.
For broker connections you could pool them and only use them when a user needs them and not tie their lifetime to the browser session at all.

Thanks Bernhard,

I actually meant the same thing. I don’t expect Server to identify the events happen at Client side directly.
But as Server does have access to the UI objects through the Session, plus with having HeartBeat process in place, I thought maybe combining these 2, there could be a separate Event in Vaadin (server side) through which we could capture semi “browser closed” actions upon listening to it. (For example, when the heartbeat failure criteria is met, then check for availability of all the UIs attached to it and if there were no accessible UI, then fire the event I just explored)

Tatu Lund:

doesn’t contain any clue regarding identifying browser or tab closed event!!

There is no event with that name. Normally you should use e.g. onDetach on mainlayout or UI::addDetachListener(listener), which should happen eventually when enough heart beats are missed and UI is wiped. Vaadin’s client part sends heart beats on regular interval, and after server misses three heart beats it concludes Browser lost and UI is marked to be wiped. The last UI is wiped after session time out provided that closeIdleSessions parameter is set to true (see also: https://vaadin.com/docs/v14/flow/advanced/tutorial-application-lifecycle.html#application.lifecycle.ui-expiration ).

If Browser is closed nicely, it can be detected, but there is lot of edge scenearios why this not reliable, however the method is decribed here

https://stackoverflow.com/questions/60084612/java-vaadin-14-detect-user-leave-closes-tab-f5-etc/60088823#60088823

So you should not rely on this alone, you need to observe UI detach as a backup anyway.

Thanks for your reply and the instructions,
I will give the JavaScript solution (explained in the StackOverflow link) a try. I think it could complement the Server Side event handling.
I understand that the nature of Browser Close event may differ in variety of browsers and I don’t expect a 100% accurate and working mechanism. but I believe it would be nice to at least have an abstraction around this by exposing an event which I have explained in my previous post (I mean apart from HB Interval and CloseIdleSessions properties).
BTW, I have used HB Interval (Set to 5 Seconds) and CloseIdleSessions (Set to true) and implemented SessionDestroyListener interface in my MainView class. but the sessionDestroy() method never gets invoked, even hours after I completely closed the browser. Did I miss something here or this is normal behavior?!