Home  >  Q&A  >  body text

Pusher passing parameters in Laravel BroadcastAs

Hi, I have this js code

var pusher = new Pusher('my pusher key', {
        cluster: 'ap2'
    });
    var channel = pusher.subscribe('my-channel');

    channel.bind('my-event', function(data)
    {
        console.log(data);
    });

This is my Laravel code

protected $pos_invoice;
public function __construct($pos_invoice)
{
    $this->pos_invoice = $pos_invoice;
}
public function broadcastOn()
{
    return new Channel('my-channel');
}

public function broadcastAs()
{
    return 'my-event';
}

This is the calling code

return event( new \App\Events\New_pos_online_order_event('aa'));

Now is the code

channel.bind('my-event', function(data)
    {
        console.log(data);
    });

always returns [] on the console, so I tried this

public function broadcastAs()
{
    return 'my-event.'.$this->pos_invoice;
}

and this

public function broadcastOn()
{
    return new Channel('my-channel'.'asdfasdf');
}

When I change anything

public function broadcastOn()
{
    return 'my-channel';
}

public function broadcastAs()
{
    return 'my-event';
}

The code does not work and returns nothing on the console So how can I pass parameters on Pusher and Laravel using js Thanks..

P粉340264283P粉340264283225 days ago433

reply all(1)I'll reply

  • P粉662802882

    P粉6628028822024-04-01 09:04:46

    You need to define the functionbroadcastWith

    **
     * Get the data to broadcast.
     *
     * @return array
     */
    public function broadcastWith()
    {
        return ['pos_invoice' => $this->pos_invoice];
    }

    You will receive the array in the data of the bound function

    reply
    0
  • Cancelreply