I am trying to build an application where whenever the client closes the browser window/tab I should send a notification to the backend to destroy the logged-in session and log the details of the running session. I have tried onbeforeunload but it is not working as intended, as it also executes on page reload. Also, it does not send the request by closing the browser tab.
As far as I know, there is no solution to do this if you do not want to destroy the session on tab reload. Also, you can't send a request on tab close. A possible solution for that would be to work with a WebSocket connection and listen for disconnect on you server and then destroy the session.