Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO: XHR-Polling delay on disconnection

I am working with socket.io and node.js. I'm am having problems tracking which users are online because of the few seconds delay before socket IO recognises an XHR-Polling client has disconnected.

If an XHR-Polling client refreshes the page their new connection message seems to precede their disconnection message. This causes confusion when trying to track which users are online.

One solution could be to detect the transport method on the serverside and delay the connection for XHR-Polling clients to ensure that the disconnection functions have been run first.

Has anyone had any experience of this?

like image 270
wilsonpage Avatar asked Jun 08 '11 14:06

wilsonpage


People also ask

Does socket IO use long polling?

js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

What is XHR poll?

Xhr-polling creates new request with http header and waits for answer with http header, also sequential order.

Does Socketio use WebSockets?

Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet.

Does socket IO reconnect automatically?

In the first case, the Socket will automatically try to reconnect, after a given delay.


2 Answers

The main problem with presence, as @davin and @jcolebrand point out in the comments, is that connections alone can't be relied upon. Network devices can hold on to connections after a user has navigated away from a page. So, you need something else in place to confirm a users presence.

This is something we've added support for in Pusher and we do this using something called Presence Channels.

We manage this using a unique user id, which you (the developer) have to supply when a user connects, in combination with a unique socket id which identifies the connection. Since this unique user id can only occur once within a presence member list it means that that user will only be listed once - even if they have multiple connections open, and thus multiple socket connections.

The same idea can be applied to HTTP connections.

The general approach for this is to use the session to uniquely identify the user. In that way even if they are just a "Guest" you can still identify them as long as they are allowing the use of cookies.

So, the number of users on your system should only ever max at the number of active sessions you have running on your server.

Hope this helps. Let me know if you'd like anything clarified.

like image 118
leggetter Avatar answered Sep 28 '22 19:09

leggetter


I'm developping a chat support solution and the attendent have to know when a client connect. With websocket works fine, but with whr-pooling, sometimes the attendent just disconnect (with 5 seconds in some cases). Sometimes dont heppend. What I do: I just enter the page and wait looking the terminal app.

I'm using nodejs, nowjs

like image 39
user840774 Avatar answered Sep 28 '22 19:09

user840774