SendWebSocketMessageEvent.php 993 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Events;
  3. use Illuminate\Broadcasting\Channel;
  4. use Illuminate\Broadcasting\InteractsWithSockets;
  5. use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
  6. use Illuminate\Foundation\Events\Dispatchable;
  7. use Illuminate\Queue\SerializesModels;
  8. class SendWebSocketMessageEvent implements ShouldBroadcastNow
  9. {
  10. use Dispatchable, InteractsWithSockets, SerializesModels;
  11. const CHANNEL = 'actions';
  12. /**
  13. * Create a new event instance.
  14. */
  15. public function __construct(private readonly string $message, private readonly int $userId)
  16. {
  17. //
  18. }
  19. /**
  20. * Get the channels the event should broadcast on.
  21. *
  22. * @return Channel
  23. */
  24. public function broadcastOn(): Channel
  25. {
  26. return new Channel(self::CHANNEL); }
  27. public function broadcastWith(): array
  28. {
  29. return [
  30. 'action' => 'message',
  31. 'message' => $this->message,
  32. 'user_id' => $this->userId,
  33. ];
  34. }
  35. }