Create multi-channels in Pusher Laravel with different events

541 Views Asked by At

I have a multi-vendor E-commerce with users making orders, and drivers making offers on these orders, I made a pusher channel so that when a user makes an order it shows on the drivers home realtime.

class NewOrderEvent implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $afterCommit = true;

    public function __construct(
        public Order $order,

    ) {
    }


    public function broadcastOn()
    {
        return new PrivateChannel('order');
    }
    public function broadcastAs()
    {
        return 'new-order';
    }
    public function broadcastWith()
    {

        return [
            'order' => OrderResource::make($this->order),
        ];
    }
}


I made another pusher channel so the user can see the offers on his order, 'order-'$orderId

class NewOfferEvent implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;



    public Order $order;
    public OrderOffer $offer;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Order $order , OrderOffer $offer)
    {
        $this->order = $order;
        $this->offer = $offer;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return ['offer-'. $this->order->id];
    }

    public function broadcastAs()
    {
        return 'new-offer';
    }

    public function broadcastWith()
    {

        return [
            'offer' => $this->offer,
        ];
    }
}

and in the .env I have the credentials of the first channel

BROADCAST_DRIVER=pusher
PUSHER_APP_ID=*******
PUSHER_APP_KEY=******
PUSHER_APP_SECRET=****
PUSHER_APP_CLUSTER=**

How to make the other channel work? My problem is that the new-offer event isn't sent in pusher because the credentials of its channel in pusher is different from the credentials I have in the .env(which are the credentials of the 'order' channel)

I tried adding another driver in the broadcasting.php like this:

 'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'host' => env('PUSHER_HOST', 'api-'.env('PUSHER_APP_CLUSTER', 'mt1').'.pusher.com'),
                'port' => env('PUSHER_PORT', 443),
                'scheme' => env('PUSHER_SCHEME', 'https'),
                'encrypted' => true,
                'useTLS' => env('PUSHER_SCHEME', 'https') === 'https',
            ],
            'client_options' => [
                // Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html
            ],
       ],



 'pusher1' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY1'),
            'secret' => env('PUSHER_APP_SECRET1'),
            'app_id' => env('PUSHER_APP_ID1'),
            'options' => [
                'host' => env('PUSHER_HOST', 'api-'.env('PUSHER_APP_CLUSTER1', 'mt1').'.pusher.com'),
                'port' => env('PUSHER_PORT', 443),
                'scheme' => env('PUSHER_SCHEME', 'https'),
                'encrypted' => true,
                'useTLS' => env('PUSHER_SCHEME', 'https') === 'https',
            ],
            'client_options' => [
                // Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html
            ],
        ],


     

    ],

But it did not work, what should I do?

0

There are 0 best solutions below