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

najeeb-anwari's avatar

Handling Presence Channel Events for Disconnections in Laravel Reverb

Hello everyone,

I'm currently developing a Laravel Reverb project and need to broadcast events when someone joins or leaves a presence channel. Specifically, I'm looking to handle scenarios where someone disconnects their internet or closes their browser window. How can I achieve this server-side? Any advice, examples, or best practices would be incredibly helpful!

Thank you in advance!

0 likes
3 replies
vincent15000's avatar

Have you tried this to detect if the browser has been closed ?

window.addEventListener('beforeunload', function (e) {
    e.preventDefault();
	// some action
});
najeeb-anwari's avatar

@vincent15000 What I want is not in front side. I actually want is to detect on server who got connected or disconnected. On server I use Laravel and Rest API. On client I use I use react.

1 like
vincent15000's avatar

@najeeb-anwari You can call the pusher API to get the connected users.

It could look like this.

use Illuminate\Http\Request;
use Pusher\Pusher;

class PusherController extends Controller
{
    public function listConnectedUsers()
    {
        $pusher = new Pusher(
            env('PUSHER_APP_KEY'),
            env('PUSHER_APP_SECRET'),
            env('PUSHER_APP_ID'),
            [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'useTLS' => true
            ]
        );

        $channelName = 'presence-channel';
        $response = $pusher->get('/channels/' . $channelName . '/users');

        return response()->json($response);
    }
}

Sure you have to use the PHP library for pusher.

https://pusher.com/docs/channels/channels_libraries/libraries/#official-server-libraries

And here you have some documentation if you need to adapt the code above to your needs.

https://pusher.com/docs/channels/using_channels/presence-channels/#accessing-channel-members

1 like

Please or to participate in this conversation.