I am adding a Node JS Chat to my website. At the moment I am clearing the user from the chat if they close the window using the following JQuery method:
$(window).unload(function () {
jQuery.get("/leave", {id: session.id});
});
This works perfectly and tells the node JS server that the user has closed the window, however this also removes the user from the chat room if they navigate to another page on the website. Is there a way I can tell if they go to another page on the domain and not remove them from the chat? At the moment when they navigate to another page it is being treated as if they are closing the window.
Cheers
Check when the user leaves the chat on the server, not the client.
The only problem I can see with this is that unless you make your pages load with JavaScript you’ll have your users leaving and re-entering the chat every time they navigate the website.
You can work around this by setting a timeout on the disconnect part. It’s not a perfect solution but it’ll work for most cases.
if you’re using
socket.iohere’s some server code:And in your client-side you can check for
action === "quit"and take the appropriate action.