In my Node app, I am using socket.io
for websocket functionality.
In order to let the client know when the server becomes unavailable, I'm using the following code:
socket.on('disconnect', function(){
//show error to client
});
This works fine, but there's a nasty side-effect. Every time I click a link to navigate away from the page (or just reload the page) the message pops up - because the client disconnects. I certainly do not wish to display a message saying 'the server is unavailable' every time the visitor clicks a link.
I could put a timeout on the error message, giving the browser time to simply navigate away before the error shows. But you'll admit this is a shabby solution. Also, there would be two problems:
1) the error message would be delayed by the timeout amount in cases when it needs to actually be displayed, leaving the user with a non-functioning app and no notification during that interval
2) it wouldn't work for page reloads (I think)
Anyway, there's gotta be a better way. Right?
Note: this is a problem in Firefox, but not Chrome. Haven't tested other browsers yet. Firefox also displays an error message in the console stating that the websocket connection was interrupted, and I'm inclined to believe these two problems have a common cause. I have read quite a few questions and answers regarding this console error, but none seem to apply to my situation.