I am developing an HTML5 application and I need to send a disconnect ajax request when the user changes or refreshes the page. Currently, I have implemented this code:
window.addEventListener("beforeunload", function(event) {
$.ajax({
url: api_disconnect,
data: { identifier: token },
method: "GET"
});
});
It is not necessary for me to process the response or ensure that the browser receives it. My main concern is whether I can trust that the server will receive the request.
If not, what is the best approach for achieving this? At the moment, my solution involves sending an "I'm alive!" request every 15 seconds (which may be excessive). Ideally, I would like the server to be immediately notified when the user disconnects.
To clarify, I understand that in cases where the browser or computer crashes, there is not much that can be done. This is why I have implemented the heartbeat mechanism. My question specifically relates to normal use cases when the user purposely closes, changes, or refreshes the page.