WebsocketEvent.php 828 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace App\Broadcasting\Events;
  3. use App\Broadcasting\Events\WebsocketEventInterface;
  4. use App\Broadcasting\Entity\WebsocketEventData;
  5. use Illuminate\Broadcasting\Channel;
  6. use Illuminate\Broadcasting\InteractsWithSockets;
  7. use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
  8. use Illuminate\Foundation\Events\Dispatchable;
  9. use Illuminate\Queue\SerializesModels;
  10. final class WebsocketEvent implements WebsocketEventInterface, ShouldBroadcastNow
  11. {
  12. use Dispatchable, InteractsWithSockets, SerializesModels;
  13. public function __construct(
  14. private readonly WebsocketEventData $dto
  15. ) {}
  16. public function broadcastOn(): Channel
  17. {
  18. return new Channel($this->dto->channelName());
  19. }
  20. public function broadcastWith(): array
  21. {
  22. return ['data' => $this->dto->data];
  23. }
  24. }