Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event broadcasting wrongly serialize payload #180

Closed
simonesada opened this issue Apr 29, 2024 · 2 comments
Closed

Event broadcasting wrongly serialize payload #180

simonesada opened this issue Apr 29, 2024 · 2 comments
Assignees

Comments

@simonesada
Copy link

Laravel Version

11

PHP Version

8.3

Database Driver & Version

MySQL 8.0 for Windows 11 (x64)

Description

The Event class is not correctly serializing the event payload. The seat object's ID is being serialized as a string instead of a JSON object.
I am using Laravel Reverb 1.0 to broadcast the event trough WebSocket and receive the message on differente clients (Chrome and Postman).

Event class:

<?php

namespace App\Events;

use App\Http\Resources\SeatResource;
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 broadcastWith(): array
    {
        return ['id' => $this->seat->id];
    }
}

JSON response:

{
    "event": "App\\Events\\SeatUpdatedEvent",
    "data": "{\"id\":\"9bec334a-9e6c-46be-a8b6-b61458eeeace\"}",
    "channel": "show-9bec334a-93d3-47ef-86f1-fabd2f97c352"
}

Steps To Reproduce

  1. Define an event that implements ShouldBroadcast interface and has a payload
  2. Connect from a client to the channel defined by the event
  3. Dispatch the event and verify the JSON response
@driesvints driesvints transferred this issue from laravel/framework Apr 29, 2024
@driesvints
Copy link
Member

Transferred to the Reverb repo.

@joedixon
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants