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

Dani AI

Generated

Short summary and practical options for the problem raised: client-side unloads are unreliable (they often won’t fire on power loss, crashes or mobile sleep — a point related to ’s suggestion), and per-user full scans are costly at scale (the concern behind the heartbeat/timestamp ideas from and ). A robust design combines a lightweight client indication of activity with a server-side mechanism that doesn’t require scanning every row for every visitor.

One efficient approach is an ephemeral presence store (Redis or Memcached). When the client is active, refresh a key with a short TTL; when the key expires the user is implicitly offline, so no manual delete is required. Example (PHP + phpredis):

$redis->setex("online:{$userId}", 30, 1);

Avoid using Redis KEYS on large datasets; instead maintain a single set or sorted set for membership and trim expired members periodically.

If you must use MySQL and CodeIgniter, run a small server-side cleanup on a schedule rather than on every request. Create a CLI controller (e.g., Cron::purge_offline) and call it from cron:

* * * * * php /full/path/to/index.php cron purge_offline >/dev/null 2>&1

If cron isn’t available, the MySQL Event Scheduler can run periodic deletes. The cleanup job should delete rows older than your chosen threshold.

Practical tips: index the last-seen column, batch deletions or use TTL-backed stores to avoid table scans, set client ping intervals and server TTL consistently (TTL slightly above ping interval), and avoid relying solely on unload for logout. For real-time bi-directional needs consider sockets, but for presence alone TTL-based presence or scheduled pruning is simpler and scales far better.

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.