Doing AJAX when browser's window is closed

Viewed 80

I have a chat app which utilizes Laravel Echo with Pusher (which is similar to Socket.io).

And the issue I'm getting here is I can't update a user online status in database if there's no single user is online on the other side

Been searching on internet and found out about unload event in Javascript, but they told that it's not reliable to handle this problem.

I also have yet to find an event on the Laravel Echo to trigger it even though Pusher records it with "Disconnection" event every time a user load the page or browser is closed

What is the most effective way to handle this?

1 Answers

I don't know about used tools. Maybe use fake user/bot to keep at least someone online always?

Regarding closing window. There are other events suitable for this. The most obvious and recommended would be beforeunload. Another option is pagehide, but it's less supported for now and has a bit different meaning.

Also navigator.sendBeacon() might be useful here as network request transport. That way browser doesn't have to wait for response.

Related