Pusher passing parameters in Laravel BroadcastAs
P粉340264283
P粉340264283 2024-03-31 20:27:44
0
1
385

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粉340264283
P粉340264283

reply all(1)
P粉662802882

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!