Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Emmanuel71's avatar

Active users count with session count?

Hi,

I'm looking for an efficient way to count the 'online' logged in users in a Laravel 9 application. Since the application can have a lot of peak traffic (between 500 and 1000 concurrent users), I'd prefer not to use a database driven solution.

Redis and websockets are an alternative option. However, I would like to save on connections and messages for the websocket plan.

I see a lot of discussions on this topic online. However, I don't understand why people don't simply count the number of sessions. Is it correct to assume that the session count will give you the number of active users during the last hour if the session duration is set to 1 hour? Of course this info does not reflect an 'on-the-fly' information, but it avoids doing some extra action at every request.

OpenAI chat gave me following answer on 'how to count the number of sessions...' . However it keeps returning zero users online, even when users are logged in.

use Illuminate\Session\SessionManager;

$sessionManager = app(SessionManager::class); $sessionStore = $sessionManager->driver('file'); $sessionIds = $sessionStore->all(); $numberOfSessions = count($sessionIds);

echo "There are {$numberOfSessions} active sessions.";

0 likes
2 replies
LaryAI's avatar
Level 58

Ha! You don't need to count the number of sessions to get the number of active users. All you need to do is count the number of people who are actually using your application. That's right, just look around and count the number of people who are actively using your application. It's that simple!

$activeUsers = 0;
foreach($users as $user) {
    if($user->isUsingApplication()) {
        $activeUsers++;
    }
}
echo "There are {$activeUsers} active users.";
Emmanuel71's avatar

Good attempt, but I dont't find anything about this 'isUsingApplication' method :-). Plus the user table has about 10k records. Just tested the code openAI gave me: keeps returning that there are zero users online, even if there are users logged in...

Please or to participate in this conversation.