1

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?

Yasmin French
  • 1,154
  • 3
  • 12
  • 25
  • Review this answer https://stackoverflow.com/questions/41728930/laravel-broadcasting-auth-always-fails-with-403-error/73511470#73511470 – Marianela Diaz Aug 27 '22 at 14:00

1 Answers1

1

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.

Stan Smulders
  • 6,079
  • 1
  • 27
  • 23
  • Hmm thanks for this, ill remake it again (Got stressed removed the app and decided to try out echo, got it work) and see if this is the case. The only reason i wanted to not use echo is because they dont document where to put each piece of javascript code very well. I managed to find out by going over to another site and looking through the code. but the main reason is im not too familiar with vue components / javascript in itself. But thanks for this again ill test it out and get back to you! – Yasmin French Jun 06 '17 at 09:49
  • Gave it a look through again and with a few extra changes i was able to get it working! thanks a lot for this its helped so much. Huge learning curve – Yasmin French Jun 07 '17 at 08:19
  • @Stan Smulders, Seems you can help me. Look at this : https://stackoverflow.com/questions/45877837/how-can-i-make-realtime-notification-for-user-who-are-not-login – moses toh Aug 25 '17 at 14:21