Add event when browser process is closing

A new event when user is closing the browser process. It can be very usefull when a service worker can be pushed or not.

What is a use-case for adding this kind of event? You should always assume a SW can be killed at any moment by the browser as a part of normal resource management.

It is a case not frequent and anyway also when you kill a process you can execute a procedure before to exiting.

Instead to send periodic keep alive every minute to server for knowing when a user is online … you can send just a ajax when it happens this event. It consumes less bandwith and not overload server with many useless requests. You can send a forced ajax request every hour. if no keepalive request after alive … user session is deleted. This case permits to handle when you kill a process. Anyway it is a case very rare. Normaly server receives just a call for each user.

You shouldn’t use a SW to keep sessions alive.

if i need to know if a user is online or nor ? how to do? Are you understanding what i m writing or you are following you thoughts? :slight_smile: I want to know when a user is online or not.Session cookies is not for to know when a user is online.I wont to know when user is in my server domain. I want simply to know when user has browser opened. Dont be confused :slight_smile:

Don’t push notifications get queued for the next time the user is online?

yes i want push message just if the user is online … not when he will be online (and it is not granted he will be online).The ttl in the queue has a limit so it is not granted it will be delivered in any case. limit can be also 3 days.

I am following what you’re saying. So either you didn’t meant what you said a moment ago or you don’t understand sessions.

navigator.onLine to check it pragmatically or you can use the online/offline navigator events to know when their network condition changes (although you won’t know when they go offline, just when they come back online.)

Using a SW to ping the server that the user is still there doesn’t seem like an appropriate use of a SW. There are numerous other ways to handle session killing that existed before SW and still work just fine.

1 Like

you are a bit confused. DanielHerr understood all. You didnt :slight_smile: . online event tell if the connection is connected, not if the browser is closed/opened. using online you could to know maybe when the user is starting the session not when he is closing it.

If you want to know when the page is open/closed you use the Page Visibility API. The browser could be up and your SW running but the user may not have an active open session on the browser side.

You otherwise have no way of knowing if the browser is open/close, as they are able to run in the background of a users system.

You continues to not understand. Page visibility tell when you minimize/mazimize the window(window where the domain is). Not when you close the browser.

There are a few APIs that already do this, like “unload” event. There is also the beacon API:

For service workers, maybe background sync would help?

1 Like

I’ve used beforeunload for this sort of last-chance-before-closing functionality:

I’m not familiar with the serviceworkers though, would those be killed before beforeunload happens?

But here there are esperts and developers or junior people? I m reading absurd comments. beforeunload tells when a user in your domain is closing the window. If my site is A , and user is going in site B , user is not closing the browser but beforeevent is triggered passing from A to B or i close tab and i go in another site. beforeunload and sendbeacon are for solving other problems.
Before to write it is necessary to read with attention the assertion of the topic.

I believe sendBeacon is not guaranteed to actually send the request before the browser exits. It is possible that it will be sent when the browser is launched next (I am not sure browsers currently implement that, but the specification lets browsers do that if they like, if I remember correctly). So that does not help in the case of Cristian.

So you want to track the online status of a browser (not of a user, since it might not have any tab open other than, for example, the Chrome “New Tab” tab and since a different person can be using the browser), right?

Did you intend for it to be a service worker event, or a regular event (to which HTML documents can listen)?

If you can come up with a solid use case, that would help stir this discussion in the right direction. So far, you mentioned that you could “push a service worker” depending on whether the browser is exiting or not. Perhaps you meant push notifications? If so, can you come up with a solid use case for sending push notifications only when the browser is running?