Home > Net >  Vaadin Flow: how to invalid a session when browser tab is closed?
Vaadin Flow: how to invalid a session when browser tab is closed?

Time:11-30

Session is invalid when browser is closed, but if the application tab is closed instead of browser, how can we invalid a vaadin flow session?

I guess the following code should be added to some place, but I don't know where. Which event is fired when browser tab is closed? Can anybody help?

VaadinSession.getCurrent().getSession().invalidate();

This piece of code is currently in logout handler and works fine.

Thanks in advance.

CodePudding user response:

The session will eventually get automatically invalidated after some period from when the last browser tab has closed. There is no need to explicitly invalidate the session. As stated in the docs:

A session is kept alive by server requests caused by user interaction with the application, as well as by the heartbeat-monitoring mechanism of the UIs. When all UIs have expired, the session still remains. It’s cleaned up from the server when the session timeout configured in the web application elapses.

If you don't want to wait for the automatic session invalidation to occur (perhaps because you want to eagerly free up some server resources), then you can also listen to UI-termination events using the Beacon API as described in this cookbook recipe, and invalidate the session manually in those UI-closed events. Notice, however, that other tabs (i.e., other UI instances) might still be open in the browser. You might, hence, want to check that there are no more UI instances associated with the session before you invalidate it (you can check the number of UIs attached with a session using VaadinSession.getCurrent().getUIs()).

CodePudding user response:

Vaadin Flow 14.8.20. I use ping component, that invoke my callback every time that ping send from browser to server. This can be used to detect that there is no active UIs in VaadinSession and close it.

PingComponent.java

import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.polymertemplate.EventHandler;
import com.vaadin.flow.component.polymertemplate.PolymerTemplate;
import com.vaadin.flow.templatemodel.TemplateModel;

/**
 * Polymer-component for page ping
 */
@Tag("ping-component")
@JsModule("./src/ping-component.js")
public class PingComponent extends PolymerTemplate<TemplateModel> {

    private final transient Runnable updatePingTsRunnable;

    public PingComponent(Runnable updatePingTsRunnable) {
        this.updatePingTsRunnable = updatePingTsRunnable;
    }

    @EventHandler
    private void invokePingEvent() {
        //System.out.println("Ping received");
        updatePingTsRunnable.run();
    }
}

ping-component.js

/**
 * Browser-side part for page ping
 *
 * @see https://polymer-library.polymer-project.org/3.0/docs/devguide/data-binding
 * @see https://polymer-library.polymer-project.org/3.0/docs/devguide/events
 *
 */
import {PolymerElement, html} from '@polymer/polymer/polymer-element.js';

class PingComponent extends PolymerElement {

    constructor() {
        super();
        /* ping send period 5000 ms */
        setInterval(() => this.invokePingEvent(), 5000);
    }

    static get template() {
        return html`
            <div></div>
        `;
    }

    static get is() {
        return 'ping-component';
    }

    // Linked to backend method PingComponent#invokePingEvent
    invokePingEvent() {
    }
}

customElements.define(PingComponent.is, PingComponent);
  • Related