I am looking for a solution to display online user/friends without using ajax (real-time) if it is possible. I want to avoid the ajax request that is made to the server after every specified time interval to check if the user is online. Currently the application works fine and displays the online users but there are huge request made to the server to check if the user is online using ajax method. Also due to huge amount of ajax calls sometimes the browser behavior is also affected. [For more information: I am using MySql database, PHP, Jquery.]
-
You're going to have to look for an update somehow - either realtime, or when the page is refreshed. – GDP May 16 '12 at 13:53
-
1You could make a socket server that clients connect to using a persistent connection... – Lix May 16 '12 at 13:55
-
You could keep a connection open to the server (like gmail). But come on, only to display online people: ajax calls are ok. Or create an iframe with an auto refresh value – Michael Laffargue May 16 '12 at 13:56
-
@MichaelLaffargue: Sometime the browser crashes due to multiple calls or due to javascript overflow. – Mahavir Munot May 16 '12 at 14:02
-
@MahavirMunot I can't see how you can have those errors but with a bad design. A such simple call should be fast as hell and you should only update it every xx seconds. Same goes for overflow, how would you arrive to that ? – Michael Laffargue May 16 '12 at 14:09
-
@MichaelLaffargue: There are 4 - 5 js plugins used but those do not affect to a great extent. However, I wish to avoid the ajax call reducing the number of request made to the server. – Mahavir Munot May 16 '12 at 14:15
-
1@MahavirMunot Well you want server information after the page is loaded, you'll have to go through requesting the server with ajax. To avoid taking 1 connection, you could create a subdomain to be called specialy for that task : refresh.domain.com for example. – Michael Laffargue May 16 '12 at 14:22
-
@MichaelLaffargue : yes, but again the browser will be making huge amount of calls [lets say 30 calls in 1 or 2 minutes]. This will also increase the load on the browser. – Mahavir Munot May 16 '12 at 14:30
3 Answers
My advice is to update a field in your database every time a user accesses a page. To determine if they are still online, check to see if this last access is within a certain time interval like 20 minutes ago. If not, assume they are offline.
This is probably the simplest method, although it isn't exactly 'realtime'. If I leave your site by just closing my browser then I won't be displayed as 'offline' until 20 minutes from then.

- 24,395
- 4
- 69
- 96
-
Please see my question again. I know the way to display the online users but to check the last access time I have to make a call to a script to get the last access time after every specified interval. – Mahavir Munot May 16 '12 at 14:05
-
No. I am saying check the last access time on _page load_ only, which means no AJAX is involved. This is a simple, naive solution to a pretty complex problem. – Jeff Lambert May 16 '12 at 14:08
-
I agree at your point but the page is loaded whenever a user clicks on any link. If the page is not refreshed/loaded then how it will work. – Mahavir Munot May 16 '12 at 14:10
-
If you're looking for a solution that works without a page load, then you're going to have to look into Server Push using Long Polling / Comet or some other technology, which some of those utilize AJAX anyway. – Jeff Lambert May 16 '12 at 14:16
-
I am aware about www.ape-project.org but there is no complete documentation/userguide available for it. Can you suggest if you know? I know it will work – Mahavir Munot May 16 '12 at 14:18
-
@Mahavir not that I know of. I've looked into APE and toyed around with it but never to the point that I personally would use it in production. – Jeff Lambert May 16 '12 at 15:16
-
I am trying to use integrate it. I am using linux Centos. Also, I doubt if memcache can do something. – Mahavir Munot May 16 '12 at 15:31
Here's a clear take on how you should do it:
- user requests page
- log the time the request was made (marking the user as active)
- get the list of friends who are active in the last < N minutes
- serve the page
Basically, every page load refreshes the user's "last seen" time and consider the user as active if the user is within a threshold. Greater than that, the user is inactive. Most chat clients mark a user as inactive after 15 mins of not doing anything, like keystrokes etc. where in our case, doing a page load.
If you want live data, there is no other (better) way than by using AJAX (interval poll and connection holding). Some crude techniques I experienced using before, other than ajax, to trigger page loads include:
- a hidden, constantly refreshing iframe, or an iframe interval loading the friend list
- interval loading of 1x1, cache-busted gifs using a routed url, so that it will pass throught the logging mechanism.
You could use WebSockets, but it's far from being cross-browser as of this time unless you use the socket capability of flash and ExternalInterfaceCall
to call JS from flash.

- 117,725
- 30
- 181
- 234
In some way or other you will have to make ajax call to the server after time interval to communicate/ get online users.

- 33
- 7