I have a table in the database which includes all active users. I then have a user list which needs refreshing to see who is the latest users online. Whats the best way to tackle this? The user list is always just who is in the active_users table. Thanks for reading.
4 Answers
AJAX
You could use an AJAX request to pull regularly the list of active users and display them on your website.
You may want to cache that list for a little while on your server in case you have a lot of users requesting it constantly.
Or, if the list is long, or if you decide to pull a lot of HTML markup with the list, you could also regularly poll your server to check whether there are updates to the (cached...) list since the last time the client updated the list. The reply will be a simple true or false, and the client will only have to request the new list when it has changed.
Meta refresh tag
You could also use the meta refresh tag, either to update the entire page, or an iframe if you don't mind iframes.
Refresh after one minute:
<meta http-equiv="refresh" content="60" />
Or with an url:
<meta http-equiv="refresh" content="60;url=http://site.com/list.php?counter=1" />
Manual update
Finally, you may just add a button or a simply a link at the end of the list, and the user could decide for herself whether to reload or not.
Other considerations
As JoeGeeky pointed out in the comments, for performance and bandwidth reasons, you may want to implement a counter which limits the maximum number of times the user list is loaded. This is valid for Ajax and Meta refresh tags. In javascript you could simply have a variable incrementing every time you load the list, and in case of the meta refresh tag you could add the counter in the url as a get variable.
Also those three approaches do not exclude each other but should be combined: Use Ajax for the folks with javascript activated, a meta refresh iframe in a noscript tag as a fallback for the ones with javascript disabled (as mentioned by stagas), and a button or link for manual update once the maximum list reload count has been reached.
-
Yes. Same problem with some other technology (you could do it with flash - but then all the users will need flash...) – marapet May 29 '10 at 09:29
-
1@Luke: The other option is probably an ` – stagas May 29 '10 at 09:30
-
Stagas this is perfect, that will be fine, a refresh every 2 minutes – sark9012 May 29 '10 at 09:40
-
1@Luke: You can also have the ` – stagas May 29 '10 at 09:50
-
1Keep in mind there is a hidden cost. People walk away from thier broswers all the time. This means you will get hours or days worth of traffic that is reaching noone and costing you bandwidth and possibly money. Make sure you set a max Reload count that only gets reset with real user interactions or when the larger page is refreshed. – JoeGeeky May 29 '10 at 15:50
-
@JoeGeeky Excellent suggestion, I added it to the answer above. – marapet May 29 '10 at 19:23
If your userlist is short, for example, you are just displaying the latest five users, I would go with a simple polling with Ajax to a PHP script that returns that data.
You can adjust the polling to whatever your needs are,
$(document).ready(function () {
function refreshUserlist () {
$.ajax({
url: "user_list.php",
success: function (data) {
// code to refresh your website with the info out of data
setTimeout(refreshUserlist, 5000);
}
});
}
refreshUserlist();
});
- 10,151
- 2
- 35
- 46
-
I would go with setInterval() and an anonymous function for something like that. Also 5000 ms is going to increase the load on the server for no reason. 1-2 minutes should be enough. Also thanks for reminding me the correct $.ajax parameters I had it wrong in my answer :) – stagas May 29 '10 at 09:37
-
Just realized that setInterval() wouldn't show the user list until the interval has passed. Yours is better :P (unless he puts an include('user_list.php') in the page's script.. hmm too much time spent on one question) – stagas May 29 '10 at 09:42
Simplest way I can think of is that you can make a simple php page that lists the data, and use jQuery .load() function to load that php's html response into a div
$("#divid").load("active-users.php");
- 11
- 1
Make a php script to retrieve the list of active_users and return a <ul> list. Then use jQuery and setInterval() to fetch that list with $.ajax() and replace the list every 1-2 minutes or so with $('#active_user_list').html(new_list_data). Here's what it might look like (untested) :
setInterval(function() {
$.ajax({ url: 'script_to_fetch_active_users_list.php', success: function(new_list_data) {
$('#active_user_list').html(new_list_data);
} });
}, 1000*120);
The php script is up to you...
- 4,307
- 3
- 27
- 28