How do popular platforms like Facebook handle user online status without causing inconvenience to users?

Popular platforms like Facebook handle user online status by implementing a system that periodically updates a user's online status in the background without causing inconvenience to users. This is typically done using AJAX requests to periodically check for user activity and update their status accordingly.

// Code to periodically update user online status
// This code can be run in the background using a cron job or a background worker

// Check if user is active (e.g. has made a request in the last 5 minutes)
$last_activity_time = strtotime($user->last_activity_time);
$current_time = time();
$inactive_time = 300; // 5 minutes

if (($current_time - $last_activity_time) < $inactive_time) {
    // User is active, update their online status
    $user->updateOnlineStatus(true);
} else {
    // User is inactive, update their online status
    $user->updateOnlineStatus(false);
}