Member-only story
Presence Channels in Laravel
2 min readDec 28, 2024
Auctibles is a sale events platform that generates a multiplayer game experience by showing playful notifications whenever users enter the event.
Such an experience can be created in the Laravel PHP framework through presence channels.
Channels
The presence of authenticated users is broadcast in Laravel on private presence channels.
In routes/channels.php
,
Broadcast::channel('room.{room}', function (User $user, Room $room) {
return ['id' => $user->id, 'role' => session('role'), 'name' => $user->full_name];
});
Listeners
In Livewire, we define listeners to the presence events:
private function presence_listeners()
{
return [
"echo-presence:room.{$this->room->id},here" => 'hereOnline',
"echo-presence:room.{$this->room->id},joining" => 'joinOnline',
"echo-presence:room.{$this->room->id},leaving" => 'leaveOnline',
"echo-presence:room.{$this->room->id},error" => 'errorOnline',
];
}
Processing Presence Information
We show a toast using livewire-toaster whenever we receive a notification on users entering or leaving an event.