z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

Broadcast private channel return error 403 #5779

Open hieulvt opened 1 year ago

hieulvt commented 1 year ago

Description:

I am building a broadcast message feature in a web (using laravel-admin package) with the scenario: When an order is confirmed, the message/notification will be sent to the admin user in private channel if order.supplier_id = admin_user.supplier_id. I use Pusher to broadcast event from server side and Laravel echo to listen channel/event on client side. I follow the laravel document to do some setting for authorizing channel but when sending request to /broadcasting/auth its alway return 403. Here is the detail in my code: My event \app\Events\OrderConfirmed.php

`class OrderConfirmed implements ShouldBroadcast { use Dispatchable, InteractsWithSockets, SerializesModels;

public $order;

public $message;

/**
 * Create a new event instance.
 *
 * @return void
 */
public function __construct(Order $order)
{
    $this->order = $order;
    $this->message = "A new order has just been confirmed. Order no #" . $order->order_no;
}

/**
 * Get the channels the event should broadcast on.
 *
 * @return \Illuminate\Broadcasting\Channel|array
 */
public function broadcastOn()
{
    return new PrivateChannel('order-confirmed.', $this->order->supplier_id);
}

}`

Listening:

var supplierId; Echo.private(order-confirmed.${supplierId}) .listen('OrderConfirmed', (data) => { console.log(data); });

\app\Providers\BroadcastServiceProvider.php

`public function boot() { Broadcast::routes();

    require base_path('routes/channels.php');
}

` \routes\channels.php

Broadcast::channel('order-confirmed.{supplierId}', function ($user, $supplierId) { return (int) $user->supplierId === (int) $supplierId; // I tried to change this to always returning true, but issue still be not fixed. });

With public channel, everything is working fine. Please help me on what was wrong with private channel.