5

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.

Community
  • 1
  • 1
scanales
  • 602
  • 1
  • 7
  • 18

2 Answers2

10

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});
Ali Ersöz
  • 15,860
  • 11
  • 50
  • 64
scanales
  • 602
  • 1
  • 7
  • 18
  • I did exactly that... 1) npm install socket.io 2)npm install socket.io-client 3) var socket = io.connect('http://yourdomain.com', {'sync disconnect on unload' : true}); on client-side. Xhr-polling works ok on Chrome browser. But not on my iPad. – ngzhongcai Aug 29 '12 at 13:53
  • Hmm not sure why the ipad's handling it differently... have you tried disabling all other transports to make sure it is happening exclusively over XHR-polling, since it's the transport we're talking about here? – scanales Aug 29 '12 at 18:21
  • Very certain, only xhr-polling in transport option – ngzhongcai Sep 02 '12 at 03:50
  • 1
    Think I will wait for engine.io to be merged – ngzhongcai Sep 02 '12 at 03:51
  • Brilliant - saved me some big trouble. Here's a very real question for you: how did you find this option? I only seem to see things like this on StackOverflow and the rest. What did you do to discover this flag? For other intrepid explorers: through some Googling, I found this page for socket.io configuration options. https://github.com/LearnBoost/socket.io/wiki/Configuring-Socket.IO – EML Apr 03 '13 at 21:51
  • @EML the option is right there on the URL you mention, under the "client" header. – scanales Apr 15 '13 at 23:06
  • Yes, that's why I posted the page. I found it easily after you presented the option name. – EML Apr 15 '13 at 23:33
  • Thank you! it really helped me.. gonna post it on forums and stuff :D – julian Jul 28 '13 at 23:50
  • @IsraelG. I'd appreciate a link to this question! – scanales Jul 29 '13 at 15:16
  • 1
    @ngzhongcai See this answer for more details on why sync disconnect does not work on iOS: http://stackoverflow.com/a/18064892/69868 – Miroslav Bajtoš Aug 09 '13 at 09:22
0

You can also get "Error: xhr poll error" if you run out of open file descriptors available. This is likely to happen during a load test.

Check the current open file descriptor size:

ulimit -n

Increase it to a high number:

ulimit -n 1000000
Deniz Ozger
  • 2,565
  • 23
  • 27