Yasmin French
Yasmin French

Reputation: 1284

Laravel Broadcasting

My Scenario

Im trying to get laravel to work with pusher without the use of laravel echo, everything works on a public channel but when i switch to a private channel in the broadcastOn() method of my event pusher on the frontend doesn't pick anything up anymore. It gets logged in my pusher applications event log as a private channel but pusher on the frontend just sais no.

I've set up and returned true for the channel code like this:

Broadcast::channel('application', function ($post, $username) {
    if(true){
        return true;
    }
});

This is my event code:

public function broadcastOn()
{
    return new PrivateChannel('application');
}

The class implements ShouldBroadcast and lastly here is my front end code:

 <script>
    //instantiate a Pusher object with our Credential's key
    var pusher = new Pusher('MY_KEY', {
        cluster: 'en',
        encrypted: true
    });

    //Subscribe to the channel we specified in our Laravel Event
    var channel = pusher.subscribe('application');

    //Bind a function to a Event (the full Laravel class)
    channel.bind('App\\Events\\PostMessage', function(){
        console.log('Event Logged');
    });
</script>

My Question

Why is my pusher front end code not detecting my private broadcast?

Upvotes: 1

Views: 1736

Answers (1)

Stan Smulders
Stan Smulders

Reputation: 6237

Hehehe I think I found it: https://pusher.com/docs/client_api_guide/client_private_channels

You need to prefix the channel name with 'private-'. One of the things Echo does automatically for you!

Here's the rest for reference. The final advice still stands and would've at least helped you with debugging this issue ;-)


A few things you want to check when using Pusher without Echo in Laravel:

  • Make sure you have your cluster set correctly. I just checked my own dashboard and had mt1 as a cluster. It could be the defaults are obsolete.

  • Echo is set up to do a lot of things out of sight. One of those is converting easy to remember strings to their full namespaced counterparts when communicating with Pusher. So if you want to skip Echo, you want to make sure you emulate this behaviour 1 on 1, or else it will break.

On that last note, it could be that you need to do something like this:

var channel = pusher.subscribe('App/Events/application');
  • Try copying the docs 1 on 1 as well, adding some code that make the channel unique. I don't know the internals, but it could be that the PrivateChannel requires a unique parameter, and doesn't function without:

Like this:

public function broadcastOn()
{
    return new PrivateChannel('order.'.$this->update->order_id);
}

And then for your channel:

Broadcast::channel('order.{orderId}', function ($user, $orderId) {
    return $user->id === Order::findOrNew($orderId)->user_id;
});

Try using Echo first, to get everything running following the docs. It provides a great layer of convenience. Then break it down from there. At least that way you know you have all the basics down and communications between your app and Pusher are working! :-)

That's all I could think of right now without actually coding it myself! Hope one of them works for you.

Upvotes: 1

Related Questions