0

I am dispatching the SeatUpdatedEvent through a websocket with Laravel Reverb. The "data" key of the JSON response is serialized twice. What am I missing?

<?php

namespace App\Events;

use App\Models\Seat;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class SeatUpdatedEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct(
        public Seat $seat
    ) {
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new Channel('show-'.$this->seat->show_id),
        ];
    }

    public function broadcastAs(): string
    {
        return 'seat.updated';
    }

    public function broadcastWith()
    {
        return ['test' => 'Hello World!'];
    }
}

JSON Response:

{
    "event": "seat.updated",
    "data": "{\"test\":\"Hello World!\"}",
    "channel": "show-9bec219c-927f-4d28-ac91-79e9a167489f"
}

I tried to test different types of payload inside the broadcastWith() method.

3
  • That is how it works, I do not understand what your issue is with it. How are you using it? Commented Apr 29 at 10:56
  • I am using Postman as client to receive the message triggered by the event. I would expect the response to be serialized correctly and not to have escape characters inside it. The "data" property of the response is handled like a string.
    – ssada
    Commented Apr 29 at 11:12
  • I still do not understand why are you having issues as the documentation does not state you have to read this data back yourself, but use packages: laravel.com/docs/11.x/broadcasting#reverb Commented Apr 29 at 17:19

0