Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io force a disconnect over XHR-polling

I have a client/server application using nodejs on the server and socket.io as the connection mechanism. For reasons relevant to my application I want to have only one active connection per browser, and reject all the connections from other tabs that may be opened later on during the session. This works great with WebSockets, but if WebSockets is not supported by the browser and XHR-polling is used instead, the disconnection never happens, so if the user just refreshes the page, this is not interpreted as a reconnection ( I have a delay for reconnection and session restoring), but as a new tab, which ends in the connection being rejected because the old connection made by this same tab is still active.

I'm looking for a way to effectively end the connection from the client whenever a refresh occurs. I've tried binding to the beforeunload and calling socket.disconnect() on the client side, and also sending a message like socket.emit('force-disconnect') and triggering the disconnect from the server with no success. Am I missing something here? Appreciate your help!

I've read this question and couldn't find it useful for my particular case.

like image 350
scanales Avatar asked Aug 20 '12 19:08

scanales


1 Answers

Solved the issue, it turns out it was a bug introduced in socket.io 0.9.5. If you have this issue just update BOTH your server and client-side code to socket.io > 0.9.9 and set the socket.io client-side options sync disconnect on unload to true and you're all set.

Options are set this way:

var socket = io.connect('http://yourdomain.com', {'sync disconnect on unload' : true});
like image 152
scanales Avatar answered Sep 22 '22 22:09

scanales