Initial commit
This commit is contained in:
46
app/Events/UserPresenceUpdated.php
Normal file
46
app/Events/UserPresenceUpdated.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
// 5. Event Broadcasting for Real-time Updates
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class UserPresenceUpdated implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public $user;
|
||||
public $guard;
|
||||
public $status; // 'online' or 'offline'
|
||||
|
||||
public function __construct($user, $guard, $status = 'online')
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->guard = $guard;
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PresenceChannel("presence-{$this->guard}-users");
|
||||
}
|
||||
|
||||
public function broadcastWith()
|
||||
{
|
||||
return [
|
||||
'user' => [
|
||||
'id' => $this->user->id,
|
||||
'name' => $this->user->name,
|
||||
'avatar' => $this->user->avatar ?? null,
|
||||
],
|
||||
'guard' => $this->guard,
|
||||
'status' => $this->status,
|
||||
'timestamp' => now(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user