Kenziiee Flavius
Kenziiee Flavius

Reputation: 1949

Laravel not consuming Pusher events

This has been a long standing problem for a while now and before I start here's what I've tried and where I've looked for a solution.

A Few Things I've Tried

I've made sure that my cluster was correct for the application that I'm using: https://laracasts.com/discuss/channels/laravel/pusher-is-not-receiving-my-message?page=1
My cluster is eu

I've tried all variation of namespaces:
https://laracasts.com/discuss/channels/laravel/echo-broadcast-event-listener-not-being-triggered .App.Events.DefaultPusherEvent, App\Events\DefaultPusherEvent, .DefaultPusherEvent etc...

My Scenario

So I'm trying to get to grips with real-time updates using Laravel and Pusher so eventually I can implement real-time notifications + whatever else we need in the future.

I got as far as being able to broadcast to my pusher application following the Laravel documentation.

Its Working...

So that's all fine no issues there, the only issue I have is actually trying to consume this event using Laravel's echo and even Pusher's vanilla code.

I have had this problem before and my solution was to not use private channels (as they didn't work at all for me). But now I need the functionality.

Progress

Before posting this question I re-made a fresh installation of Laravel and followed it through again, but the outcome was the same. I even made a login system to make sure it wasn't because my users wasn't because my users weren't logged into the application.

ListenForPusherEvents(){
    Echo.private(`datalev`)
        .listen('DefaultPusherEvent', (e) => {
            console.log(e);
            console.log('Something');
        });
}

Also I have NO console errors as I've already authenticated the user using pushers 'broadcasting/auth' directory

This is the code that authenticates the user for the private channel.

public function PusherAuthentication(Request $request)
{
    $pusher = new Pusher(env('PUSHER_APP_KEY'), env('PUSHER_APP_SECRET'), env('PUSHER_APP_ID'), ['cluster' => 'eu']);
    $pusher->socket_auth($request->input('channel_name'), $request->input('socket_id'));
}

Upvotes: 1

Views: 1328

Answers (1)

David Heremans
David Heremans

Reputation: 671

Make sure your broadcasting/auth route correctly returns a JSON object with the auth key. This is used by Pusher to authenticate your subscription to the channel.

The process is as follows:

  1. your client wants to subscribe, and asks Laravel if it's okay (the /broadcasting/auth AJAX call)

  2. your Laravel confirms it's okay and informs pusher; you're getting a unique auth key that is sent back to your client through AJAX

  3. your JavaScript can then subscribe to the Pusher channel using that auth key it received from your Laravel, and which is known as a valid key by Pusher, so Pusher will accept it.

Upvotes: 3

Related Questions