#Check if user has leaved Angular Application (either closed the tab or browser)

6 messages · Page 1 of 1 (latest)

obtuse stag
#

Hello, I'm working on a logout feature in my application and for that, I need to check if user has leaved my application and so call an Http Request to finish the API session. I've searched a lot on the topic and couldn't get too much. What I'm currently doing is using @HostListener decorator to listen a "beforeunload" event, but yet, that is not the properly functionality we wanted. Take a look:

  @HostListener('window:beforeunload', ['$event'])
  beforeUnloadHandler(event: BeforeUnloadEvent) {

    // *trying to stop showing the popup;
    // event.preventDefault();
    // event.stopImmediatePropagation();

    // calling my logout function()
    this.myLogoutFunction(event);

    return false;
  }

the first problem using this method is that it is being called when the page is reload and that cannot happen. It should be called just and only when either the tab or browser is closed. The second problem is a popup asking "You sure want to leave site?". We do not want to be asked, but I didn't figured out how to disable this popup.

this is quite a big problem to me and I'm not getting to solve it. Very very thank you if someone could help me 🙂

ionic sparrow
#

it's not possible to do a HTTP request when you close the tab/browser.

If you the server need to know when someone left the page, one solution would be to use Socket.io

obtuse stag
#

Could you explain me why is it not possible? I really thought it was because isn't it common in applications? thank you

wheat flame
#

All you're doing with a website is sending an HTML to a user that displays things you made. You don't have an active connection. Thus you can turn off your internet after you gone to a website and you can still see the website.

What you could do is setup an inactivity timer for however long you think is appropriate and if your API doesn't get a call after so long you do what you need to do.

#

example have a API call go off every x time and if the API doesn't receive it then disconnect

#

/logoff