feat: chat en tiempo real bidireccional funcionando con Reverb
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Message;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
|
||||
class ChatMessageSent implements ShouldBroadcastNow
|
||||
{
|
||||
public $message;
|
||||
|
||||
public function __construct(Message $message)
|
||||
{
|
||||
$this->message = $message->load('user');
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel(
|
||||
'chat.' . $this->message->conversation_id
|
||||
);
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'ChatMessageSent';
|
||||
}
|
||||
}
|
||||
+21
-12
@@ -2,30 +2,39 @@
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Message;
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class MessageSent implements ShouldBroadcast
|
||||
class MessageSent implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public $message;
|
||||
public function __construct(public Message $message){}
|
||||
|
||||
public function __construct($message)
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
$this->message = $message;
|
||||
return [
|
||||
new Channel('chat.' . $this->message->receiver_id),
|
||||
];
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return new Channel('chat');
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'MessageSent';
|
||||
return [
|
||||
'id' => $this->message->id,
|
||||
'body' => $this->message->body,
|
||||
'attachment' => $this->message->attachment,
|
||||
'attachment_type' => $this->message->attachment_type,
|
||||
'created_at' => $this->message->created_at->format('H:i'),
|
||||
'sender' => [
|
||||
'id' => $this->message->sender->id,
|
||||
'name' => $this->message->sender->name,
|
||||
'avatar' => $this->message->sender->profile_photo_url ?? null,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
|
||||
class UserStoppedTyping implements ShouldBroadcast
|
||||
{
|
||||
public $conversationId;
|
||||
public $user;
|
||||
|
||||
public function __construct($conversationId, $user)
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('chat.' . $this->conversationId);
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'UserStoppedTyping';
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class UserTyping implements ShouldBroadcast
|
||||
{
|
||||
public $conversationId;
|
||||
public $user;
|
||||
|
||||
public function __construct($conversationId, $user)
|
||||
{
|
||||
$this->conversationId = $conversationId;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function broadcastOn()
|
||||
{
|
||||
return new PrivateChannel('chat.' . $this->conversationId);
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'UserTyping';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user