Quote:
Originally Posted by 2MuchMark
(Post 22490377)
What's a good way to check browser connection with server to see if its still active?
For example: Let's say you have a bunch of people connected to a page on your website, and you want to know in real time or near real time, who by username is connected, and when they click away.
Suggestions appreciated!
|
TL;DR: Use ajax polling unless you need your server to send real-time updates to browsers. If you need to send real-time updates to browsers, use websockets.
First: The browser connection with your server closes the moment the HTTP request is completed. When you load a web page, as soon as the page is loaded, your connection with the server is closed. Nitpicky distinction, but it's important to understand it.
Now, for your use case, there's two solutions, both of which have already been suggested: Ajax polling and websockets. Here's a high-level breakdown.
Ajax polling doesn't keep a connection open with your server. It's just a little setTimeout loop that creates a NEW connection (ajax request) with your server every couple seconds. Your server can only communicate with users during the ajax request.
Websockets keep an actual TCP connection open with your server. Data can flow both ways, continuously and freely. Your server can communicate with users whenever it wants.
The big difference here is that, with ajax polling, your server can only communicate with the browser in its responses to each ajax request. With websockets, the server can communicate with the browser at any time. Think about a chat room: with websockets, the server can send chat messages to all users the moment the messages arrive. With ajax polling, the server can't send messages to users until the users make their ajax request (at which point the server sends the messages in the response to the ajax request)
Ajax polling is compatible with (basically) all browsers and any server. Websockets are not supported in older browsers, and require an async server like nodejs or tornado. Ajax polling is much easier to implement than websockets (websockets aren't really that difficult, but it's not exactly trivial to get them communicating with your server properly)
Based on the use case you've described, I would use ajax polling. Your server doesn't need to send notifications/updates to users in a continuous, real-time fashion. You just need browsers to tell your server if the page is still open.
So, IMO, stay away from websockets for this. Unless there is part of your use case that you haven't stated here, that involves the server needing to send real-time updates to users. If your server needs to send updates to users, but it doesn't need to be real time (for example, maybe a 3-second delay is acceptable), ajax polling will be adequate.
Also, as was suggested in this thread: when you store this information, avoid disk writes. If you have a thousand users on your site, all making ajax requests every couple seconds, you would be generating hundreds of disk writes per second. Not good. The information you're collecting from browsers here, it needs to be stored in memory and written to disk later, in batches.
Good luck.