• Detect if user is online?
    15 replies, posted
Short question: How can I check if a user is online? I've thought about making an AJAX request on leaving the page but is that a good idea?
keep a websockets connection open [editline]27th January 2012[/editline] when it closes, the user has left the page
[quote]I've thought about making an AJAX request on leaving the page but is that a good idea?[/QUOTE] Technically, you can't.
[QUOTE=TerabyteS_;34398766]Technically, you can't.[/QUOTE] it works in most browsers
Set an image (it's best to have it already in the DOM) src to the log out file on document unload. One way ajax request. Although I dunno if it would load in time. It sounds hacky but it may work.
nah just do an ajax request, it works fine
If you do use an ajax request remember to set async to false.
that just pisses off the user, don't do that
It's unlikely to finish if you don't. Tabs close instantly. There's always cons.
Would a check for the time of the last page request do? Cons would be it isn't accurate and someone can just leave a tab open for a long time. Pro would be it's the fastest because I'm already logging it. Maybe an additional request for when the page gets closed?
Depending on the service, you could use javascript to check if the mouse is moving, scrolling is being performed, or if keys are being pressed if it's a game. Then send an ajax request if either has happened in the last minute or so (so the request keeps them logged in). Depending on what you're doing you could decide on what is the right thing to check. Although I must admit a websocket would make this really simple.
[QUOTE=jaybuz;34399398]It's unlikely to finish if you don't. Tabs close instantly. There's always cons.[/QUOTE] it doesn't matter if the server gets a chance to respond to the client. all you care about is that the request makes its way to the server - which it will.
I've bumped into a questionable problem: If I make an AJAX request at leaving a page and the user goes to another page on the same site, what if the AJAX request arrives [B]after/B] the next page request? That would set the status to offline while the user is still there.
Use a database, and make a simple request to the page (or in your main page logic) updating a users "lastSeen" timestamp. Do this using ajax in a loop every 5th minute or so (or every single minute, however accurate you want it). Make your "is online" logic based on SELECT `last_seen`, `uid` FROM `users` WHERE `uid` = '1234' AND `last_seen` >= DATE_SUB(NOW() INTERVAL 5 MINUTE) If you receive an empty result, the user isn't online, but if you DO receive a result, the user was seen recently. This method is able to get all online members very easily as well, by simply removing "uid" from the WHERE claus. Sure, if the user logs off in this case, it'll take 5 minutes until your logic notices, but then you can simply change your logic to use another interval. This will be simple to do in JS, and doesn't require you to keep an active web socket / server, and considering this is all server sided, it'll work in every single browser.
[QUOTE=h2ooooooo;34458294]Use a database, and make a simple request to the page (or in your main page logic) updating a users "lastSeen" timestamp. Do this using ajax in a loop every 5th minute or so (or every 1st, however accurate you want it). Make your "is online" logic based on SELECT `last_seen`, `uid` FROM `users` WHERE `uid` = '1234' AND `last_seen` >= DATE_SUB(NOW() INTERVAL 5 MINUTE) If you receive an empty result, the user isn't online, but if you DO receive a result, the user was seen recently. This method is able to get all online members very easily as well, by simply removing "uid" from the WHERE claus. Sure, if the user logs off in this case, it'll take 5 minutes until your logic notices, but then you can simply change your logic to use another interval. This will be simple to do in JS, and doesn't require you to keep an active web socket / server, and considering this is all server sided, it'll work in every single browser.[/QUOTE] Yeah, I think I'll just go with that.
[QUOTE=h2ooooooo;34458294]Use a database, and make a simple request to the page (or in your main page logic) updating a users "lastSeen" timestamp. Do this using ajax in a loop every 5th minute or so (or every single minute, however accurate you want it). Make your "is online" logic based on SELECT `last_seen`, `uid` FROM `users` WHERE `uid` = '1234' AND `last_seen` >= DATE_SUB(NOW() INTERVAL 5 MINUTE) If you receive an empty result, the user isn't online, but if you DO receive a result, the user was seen recently. This method is able to get all online members very easily as well, by simply removing "uid" from the WHERE claus. Sure, if the user logs off in this case, it'll take 5 minutes until your logic notices, but then you can simply change your logic to use another interval. This will be simple to do in JS, and doesn't require you to keep an active web socket / server, and considering this is all server sided, it'll work in every single browser.[/QUOTE] And why would you need more than a 5 min precision.
Sorry, you need to Log In to post a reply to this thread.