I'm creating a chat system and I need to remove every user that did not properly logout (if for instance the computer was shutdown unexpectedly,...), such as some other tasks.
One possibility is for each user's script check for inactivity (3 seconds, for instance) of every other user, but if there are many users online I think I may get in trouble on performance.
Is there an easier way to do this? Is it possible to run a script automatically on the server that performs such task? how?

My thanks in advanced

Recommended Answers

All 5 Replies

Could you maybe have some use with Javascript's unload function so when it is used it will send a query to the server telling it the user is unavaliable and it will log them out.

Thank you for your response.
But does the Javascript's unload function work when a computer is abruptly turned off?

Try it, I'm not sure if it does that. I only know of it working when the user closes the web page. You can still try it.

when any user sends a messsage to server; mark its activity and to show who is online or offline you might be checking it by any type of AJAX call with the setInterval function of the javascript. So on the server end of that AJAX you just put the time difference constraint with your query like DATEDIFF(startdate, enddate, units) (i am not sure about the exact function call of the SQL).

Now each time when the user loggs in; check for any previous login activity entry. If they exist; fire the DELETE query and the INSERT new on; and if it doesnt fire the INSERT directly.

Over the past I have used two main ways (with some variations) that really worked with application that needs to know who is online.

The first is quite simple … an online visitors table with a code (could be a hash with internal salt of a session id if the online status has to do with session or not if it hasn’t – Or even a random code – session id map ). Then expose it as variable to JS and call an AJAX listener every X seconds updating the TIME_STAMP field of this table with this code. To get who is online with that method is as simple as selecting everyone that has TIME_STAMP > than the last X seconds.

The second one was an experiment but nowadays (hopeful) works in almost every browser (the big ones tested). It is web sockets … it is a bit complex right now and it will always have a resource footprint on the server but it is the logical way to go if you really want to open a socket among your site and a visitor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.