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

karo0420's avatar

Laravel Reverb , keep track of online users

Hello guys Hope you are doing great.

I have realtime service for notifications to deliver to user.

BUT: I need: if the user is offline save the notification to database and sned to the user after online , if online send it straight away to user.

I just confused what to do. is there any hook in Reverb to detect if user is online or not? is there any way to guarantee the messages not lost? (ignore message brokers please) searched about it but i can see people suggest presence channel to keep track of online users, but it seems in client side in laravel Echo , all users can see all the online users in .here(users) => {}.

thank you :)

0 likes
4 replies
omeratayilmaz's avatar

Hello,

To manage notifications effectively in Laravel Reverb while tracking online users, you can use presence channels combined with a database to store notifications. Here’s a step-by-step approach:

Step 1- Presence Channels (channels.php)

in channels.php file; define a presence channel to track user presence

Broadcast::channel('foo-channel.{userId}', function ($user) {
    return ['id' => $user->id];
});

Step 2- Detect Online Users

use Echo on the client side to detect online users when they join the presence channel.

# there might be better solutions
Echo.join(`foo-channel.${userId}`)
    .here((users) => {
        console.log(users); // Online users
    })
    .joining((user) => {
        console.log(`${user.name} has joined`);
    })
    .leaving((user) => {
        console.log(`${user.name} has left`);
    });

Step 3- Handle Notifications (Offline/Online) check if the user is online. If they are, send the notification directly; if not, store it in the database.

# simple approach, but probably enough to what you want to get
p f sendNotification($userId, $msg)
{
    $u= User::find($userId);

    if ($this->isUserOnline($userId)) {
        Notification::send($u, new YourNotification($msg));
    } else {
        $this->storeNotificationInDatabase($userId, $msg);
    }
}

Step 4- Reconnected Users: When a user reconnects, check for any missed notifications in the database and send them

1 like
omeratayilmaz's avatar

Note: That is just a basic approach; you could find a better way to do this

karo0420's avatar

@omeratayilmaz thanks for reply mate.

but you see that , BIG problem in Echo.here , all the users can see online users -> if i make presence chaneel and all the users join it, maybe 200,000 users, all users will see these 200,000 users and every online/offline will inform them which is not efficient.

and you mentioned sendNotification(userId, $msg) function: Question -> What is $this class? and how you get the online user with isUserOnline method? exactly this is my question in fist post :))

not a good approach , there should be a better way to handle this.

thanks

1 like
omeratayilmaz's avatar

@karo0420 Then what about group presence channels? Or, as another solution, maybe you can use Redis with TTL to check if users are online. I don’t know, maybe we just need a bit of brainstorming =)

Please or to participate in this conversation.