socket.io force a disconnect over XHR-polling
Asked Answered
D

2

6

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.

Destrier answered 20/8, 2012 at 19:30 Comment(0)
D
11

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});
Destrier answered 21/8, 2012 at 16:19 Comment(10)
I did exactly that... 1) npm install socket.io 2)npm install socket.io-client 3) var socket = io.connect('yourdomain.com', {'sync disconnect on unload' : true}); on client-side. Xhr-polling works ok on Chrome browser. But not on my iPad.Pigg
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?Destrier
Very certain, only xhr-polling in transport optionPigg
Think I will wait for engine.io to be mergedPigg
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. github.com/LearnBoost/socket.io/wiki/Configuring-Socket.IOMedlar
@Medlar the option is right there on the URL you mention, under the "client" header.Destrier
Yes, that's why I posted the page. I found it easily after you presented the option name.Medlar
Thank you! it really helped me.. gonna post it on forums and stuff :DDisguise
@IsraelG. I'd appreciate a link to this question!Destrier
@Pigg See this answer for more details on why sync disconnect does not work on iOS: https://mcmap.net/q/833217/-socket-io-xhr-polling-disconnect-eventSparling
B
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
Boudreau answered 30/1, 2014 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.