Keep track of disconnected users on Laravel with Reverb or Pusher
In Laravel, there isn't a built-in way to keep track of disconnected users when using real-time communication libraries like Reverb or Pusher. However, we can implement this functionality by using a combination of Laravel's broadcasting feature and the libraries' APIs.
First, let's set up Laravel's broadcasting feature. We need to install the pusher/pusher-php-server
package to use Pusher as our broadcast driver. To install it, run the following command in your terminal:
composer require pusher/pusher-php-server
Next, we need to configure the Pusher driver in our config/broadcasting.php
file:
'broadcasting' => [
'default' => 'pusher',
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true,
'useTLS' => true,
],
],
],
],
Now, we can create an event and a listener to broadcast a message to all connected clients when a user disconnects:
// app/Events/UserDisconnected.php
namespace App\Events;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
class UserDisconnected
{
use Dispatchable, InteractsWithSockets;
public $userId;
/**
* Create a new event instance.
*
* @param int $userId
* @return void
*/
public function __construct($userId)
{
$this->userId = $userId;
}
}
// app/Listeners/UserDisconnectedListener.php
namespace App\Listeners;
use App\Events\UserDisconnected;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Broadcast;
class UserDisconnectedListener
{
use InteractsWithQueue, ShouldQueue;
/**
* Handle the event.
*
* @param \App\Events\UserDisconnected $event
* @return void
*/
public function handle(UserDisconnected $event)
{
Broadcast::channel('user.disconnected', function ($user) {
return $user->id === $event->userId;
});
Broadcast::channel('app', ['user.disconnected']);
Broadcast::to('app')->emit('userDisconnected', ['userId' => $event->userId]);
}
}
In the UserDisconnectedListener
class, we use Laravel's broadcasting feature to emit an event to all connected clients when a user disconnects. We also create a new channel called user.disconnected
and add it to the app
channel, which is broadcasted to all connected clients.
Now, let's set up Reverb as an alternative to Pusher. The process is similar to setting up Pusher, but we need to install the reverb/reverb-php
package instead:
composer require reverb/reverb-php
Next, we need to configure the Reverb driver in our config/broadcasting.php
file:
'broadcasting' => [
'default' => 'reverb',
'connections' => [
'reverb' => [
'driver' => 'reverb',
'key' => env('REVERB_APP_KEY'),
'secret' => env('REVERB_APP_SECRET'),
'options' => [
'app_key' => env('REVERB_APP_KEY'),
'app_secret' => env('REVERB_APP_SECRET'),
'cluster' => env('REVERB_APP_CLUSTER'),
],
],
],
],
The rest of the process is the same as with Pusher. We create an event and a listener to broadcast a message to all connected clients when a user disconnects.
With this setup, we can keep track of disconnected users in Laravel using either Reverb or Pusher. Whenever a user disconnects, an event is broadcasted to all connected clients, allowing them to update their UI accordingly.