0

Users on my website can have friends, and friends can chat with each other. You can also set your status to online, away and busy.

If a user is no longer on the website, his status should be set to offline. I guess the best way to do this would be to send AJAX requests to the server at a regular interval, then automatically set the status to offline if the server stops receiving these messages.

However, what would be a good interval to send these messages? 5 seconds? 1 minute?

If it really depends on how capable the webserver is, is there a way to check with PHP? What would be example intervals for some frequently-used webserver capabilities?

Darkwater
  • 1,358
  • 2
  • 12
  • 25
  • I think it will depend on how accurate you want it to be. How much time do you want to let your users wait for a new update. For a chat session 15-30 secs seems ok to me. There is no definite answer to this question. – Green Black Mar 12 '13 at 20:16
  • I would do it the other way around. Send a XHR to set his/het status offline. And be sure to cover cases like browser crashes and switching pages `body onunload`. On the server side a session could expire. – Bart Mar 12 '13 at 20:22
  • @Bart I thought about sending an ajax request onbeforeunload, but then it could appear to flicker between offline and online. – Darkwater Mar 12 '13 at 20:25
  • Why would it flicker? If you send for example `http://localhost/status/john/offline` it should set the state and not toggle between them. – Bart Mar 12 '13 at 20:27
  • If you would go to another page, but on the same website, it'd call onbeforeunload, while it's actually just switching pages. (The friendslist is visible on (almost) all pages) – Darkwater Mar 12 '13 at 20:29

2 Answers2

2

I would consider using this >> http://www.bedroomlan.org/coding/detecting-%E2%80%98idle%E2%80%99-and-%E2%80%98away%E2%80%99-timeouts-javascript

Then modify the following:

setIdleTimeout(2000); // 2 seconds
setAwayTimeout(4000); // 4 seconds
document.onIdle = function() {$('#div_idle').css('opacity', '1');}
document.onAway = function() {$('#div_away').css('opacity', '1');}
document.onBack = function(isIdle, isAway) {
    if (isIdle) $('#div_idle').css('opacity', '0.2');
    if (isAway) $('#div_away').css('opacity', '0.2');
}

to something like:

setIdleTimeout(120000); // Make these two 
setAwayTimeout(360000); // a lot longer
document.onIdle = function() {
    // your ajax calls for when they go
}
document.onAway = function() {
    // code here to maybe log them out or what ever you wish to do via ajax.
}
document.onBack = function(isIdle, isAway) {
    // code here for when they return
}

This way you cut down on the requests.

QUICK EDIT:

Logging out when closing the browser can be achieved via jQuery .unload():

$(window).unload(function() {
  // ajax call when they just go, like they don't even care!
});
Andrew Myers
  • 451
  • 3
  • 13
2

This is really more of a usability/expectations question than a coding problem. If the load on the server isn't very high, these aren't even going to be noticeable. I doubt you'd need to check every 5 seconds, but it is probably feasible, especially if it's just a personal site with a limited number of users. In high load conditions, you'd probably make it substantially higher.

Randy Howard
  • 2,165
  • 16
  • 26