Vaadin 7 - reliable close detection?

I’ve been trying to get Vaadin 6 to consistently handle app/window closing so that I can clean up app threads and resources and such. Notification of close events seem to be rather dependent on the browser type. Also, it does not seem to be very reliable, as evidenced by the forum links regarding this subject and suggestions to use timers and refreshers and such.

My question: Will Vaadin 7 do a better job of detecting when an app (i.e. a Root) closes? If not, perhaps Vaadin 7 should include some means of reliable detection of closure across browsers, such as including a heartbeat or some such mechanism (instead of everyone needing such reliable notification having to roll their own).

Vaadin 7 will still have to face the same realities as Vaadin 6: Some browsers do not provide any events when a window/tab is closed. There’s no way of sending the information to the server if the user is offline when closing the app. If the browser or the computer crashes, no event will be sent.

Vaadin 7 will most likely use some kind of heartbeat for detecting which Roots are still active. There are still some fundamental questions that remain unanswered, e.g. how many missed heartbeats there should be before the Root is considered closed and how this relates to the Servlet (or Portlet, just to make things interesting) session timeout.

Sounds good. Thanks.

BTW: Since close events do not work consistently across browsers would it make sense to deprecate this feature in Vaadin 7, and just support the reliable/heartbeat behavior? (One of the things I like about the Vaadin approach is that it assures (or at least tries to assure) consistent behavior across browsers.)


I can’t seem to get the heartbeat to work as

I have set in my web.xml the values below

heartbeatInterval 1 close-idle-roots true

Even if i close the browser, the cleanup listener doesn’t seem to be able to catch any clean up event.

Any idea?

The cleanup listeners are invoked for each inactive UI
on each request
- there is (at least currently) no background thread doing periodical cleanup. You should see the listeners run if you open a Vaadin application, close the tab/window, wait for 3*heartbeatInterval seconds and then open the same page again. Currently, there is a regression that prevents cleanup from being run on heartbeat requests themselves. Ticket
tracks this issue, should be fixed in beta 3.

Vaadin 7 currently does not support CloseListeners at all; only CleanupListeners that get run when/if the UI is eventually disposed of. The deprecated LegacyWindow class could in principle support close listeners, but thus far it seems nobody has indicated they’d like to retain the feature.

Hi Guys,

I tried to implement the Cleanup Event using beta3 now but unfortunately it is never fired. Do I miss a thing?

import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.UI.CleanupListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

public class RootApplication extends UI implements CleanupListener {
    protected void init(VaadinRequest request) {
        VerticalLayout view = new VerticalLayout();
       	view.addComponent(new Label("Hello Vaadin");
	public void cleanup(CleanupEvent event) {

And my Servlet in web.xml

    <servlet-name>My Vaadin App</servlet-name>
      <description>Vaadin UI</description>

all the best

Hi, did you check out my
other message
in this thread?

yep, I did, but thanks. I misundertstood this message and thought Ticket #9770 will fix/introduce the background thread.

But how can I detect if a browser window is closed. The UI and the session is still alive after closing the browser window.

all the best

Unfortunately, it is not possible to reliably detect the closing of a browser window on the server side due to browser support issues. The feature is available in Vaadin 6 but was removed in Vaadin 7 as was never reliable, and our policy is that all Vaadin features should work on all the supported browsers. It should be possible to write an add-on that could be used if full browser compatibility is not required. We could even offer an official add-on if there’s sufficient nterest.

I do not see CleanupListener or CleanupEvent in RC1. What happend to them in RC1?

They were removed in beta10 in favor of AttachListener/DetachListener. See ticket
for details.

Sorry for bumping this old thread.

Can you tell me what is currently the best solution for getting browser close events in Vaadin 7.0.7?
In Vaadin 6 we registered for Window.Close events, but the UI class does not provide this.

Are detachListeners the way to go?

Yes. In Vaadin 7 you should use UI.addDetachListener(…) to get the Browser close event (will be fired when session times out or heartbeat invalidates it before the session timeout is reached)


and if I use new Vaadin 7.1 with push enable, how can I get the Browser close event?

Push should not affect this as far as I know - when the UI or the session is cleaned up (sometime after the browser window has been closed, depending on timeouts and whether there are new requests), there is a UI detach event.

Thanks for response,

But when I close the browser window with push enable no detach event is fired.
The same code work fine if push is disabled

I tried to add a SessionDestroyListener but it doesn’t work if I have push enabled (it is never fired after close the browser, wait timeout setted to 1 minute and wait 3 * heartbeatInterval setted to 10sec). Why?
The same code with push disabled works, the SessionDestroyListener is fired.

I just tried to convert my Application which used Vaadin 7 Polling to use the built-in Push-mechanic and i also ran into the same problem. The Detach-Event never gets fired when Push is enabled.

Automatic Push (streaming)
heartbeatinterval: 5
sessiontimeout: 1
Vaadin Version: 7.1.0
(Project converted from 7.1.0.beta by copying a fresh 7.1.0 final pom.xml)

Did anybody find any working solution/workaround?
Also: Is there a ticket for it or is there none needed as it’s not a bug?


PS: I made a few more tests and got it to work in my case. I just tried to get the detach event in a wrong way (Through @Override void detach instead of UI.addDetachListener).
The only thing that’s a bit weird is that it was executed about 2 minutes after i closed the browser although had the timeout set to 1 minute and the heartbeat to 5 seconds.
So the heartbeat seems to not work when push is enabled (It did when i used polling)

I am struggling with similar clean up issues in my application, after realizing that just adding a detach listener on a particular component is not always enough (for example, press the refresh button on the browser).
I would love to see better documentation of all the operations an application needs to perform in order to ensure no background threads and timers are left running upon:

  1. Component detach.
  2. Session close.
  3. Navigate away
  4. Broser window/tab close