Pusher on route not sending event in Laravel

2019-07-19 13:53发布

问题:

I am creating a test app using Pusher for real-time notification on Laravel 4. As I am testing the said API, I am having difficulties on making it work.

I have this on my routes.php:

Route::get('pusher-test', function(){
    return View::make('pages.test');
});
Route::any('test', function(){
        $pusher = new Pusher('key','sect','app_key'); //my keys are correct.
        $pusher->trigger('notificationChannel', 'userRegistration', []);
});

Then my pusherTest.js file:

(function(){
    var pusher = new Pusher('key');
    var channel = pusher.subscribe('notificationChannel');
        channel.bind('userRegistration', function(data){
            $('.test').append('HEY!');
        }); 
})();

My view page:

<html>
    <head>
        <meta charset="utf-8">
        {{ HTML::style('_/css/bootstrap.css') }}
        {{ HTML::style('_/css/mystyle.css') }}
    </head>
    <body>
        <h2>HI!</h2>
        <div class="test"></div>
        {{ HTML::script('_/js/bootstrap.js') }}
        {{ HTML::script('_/js/jquery.js') }}
        {{ HTML::script('_/js/pusher/pusher.min.js')}}
        {{ HTML::script('_/js/pusherTest.js')}}
    </body>
</html>

When I try to observe the Debug Console of my app on Pusher.com, here's what I see:

But when I hit the test route, it is not sending an event on my Pusher app, neither it sends the API message to my client. But if I will use the Create new Event tester on debug console, sure enough it sends the API message and my client receives and updates it.

What do you think is happening why on my route it can't send the event to my pusher app? I can't figure it out because is has no exception error. Please advice.

回答1:

There are three points at which you can debug your integration with Pusher:

  1. Your interactions with Pusher's HTTP (REST) API
  2. Ensuring events are being received by Pusher using the Pusher Debug Console
  3. Ensuring your client integration is working by checking pusher-js logging

In your situation you've done 2. and you can see the events you are triggering aren't reaching the debug console. So, you need to do 1.

The Pusher PHP Server library provides details of how to enable debugging: https://github.com/pusher/pusher-php-server#debugging

And how to get log information from the library: https://github.com/pusher/pusher-php-server#logging

Once you have the pusher-php-server library logging information please feel free to post it so we can see what the problem may be e.g. are you getting a non 2xx response code?



回答2:

for me it was because I used the eu server and laravel is configured automatically on us server



回答3:

To expand on @adutu's answer, you need to set

'cluster' => 'eu'

Inside the 'options' section of the Pusher driver configuration, in broadcasting.php.



回答4:

The issue with this is due to the fact that you cannot set the host for the pusher broadcasting.

You only get this issue when you are using the EU cluster, in which case, you should change the host to:

api-eu.pusher.com

Similarly, you can make a custom broadcast driver. that does the same thing as the default one but sets the host parameter.

I am going to suggest an edit to the illuminate broadcast manager to make this change easier to make.