Laravel事件广播与Pusher不兼容。

7

我在我的项目中使用了Pusher。我按照Laravel文档的要求配置了广播。当我触发事件时,Pusher并没有为我工作。但是当我从Pusher控制台发送数据时,Pusher会接收到这些数据。

我还尝试了vinkla/pusher。它可以正常工作,但是Laravel事件广播无法工作。

请帮助我。

这里是我的TestEvent.php代码:

namespace Factum\Events;

use Factum\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class TestEvent implements ShouldBroadcast
{
    use SerializesModels;

    public $text;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($text)
    {
        $this->text = $text;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['test-channel'];
    }
}
2个回答

17

我遇到了类似的问题,通过逐步排查并解决它。

我假设你正在运行 Laravel 5.3。以下是一份逐步指南,可能会有所帮助:

  1. 检查你的配置文件,在 config\broadcasting.php 中:

'connections' => [
   'pusher' => [
     'driver' => 'pusher',
     'key' => env('PUSHER_KEY'),
     'secret' => env('PUSHER_SECRET'),
     'app_id' => env('PUSHER_ID'),
     'options' => [
                         'cluster' => 'eu',
                         'encrypted' => true,
                      // 'host' => 'api-eu.pusher.com'
                      // 'debug' => true,
                  ],
    ],
],
  • 在你的web.php文件中创建一个用于测试的路由:

  • Route::get('/broadcast', function() {
        event(new \Factum\Events\TestEvent('Sent from my Laravel application'));
    
        return 'ok';
    });
    
    在你的TestEvent.php事件文件中,你应该添加这个方法来指定你的事件名称:
    /**
      * The event's broadcast name.
      *
      * @return string
      */
    public function broadcastAs()
    {
        return 'my_event';
    }
    
  • 打开你的Pusher控制台,并进入调试控制台。 保持页面打开,这样你就可以注意到是否从你的应用程序中得到了成功的请求。

    1. 启动或重新启动你的队列工作器。这一步非常重要,如果你正在使用 Mysql表作为队列,我会假设你已经设置好了你的“jobs”和“failed_jobs”数据库表,这是队列所必需的。 另一个重要的元素是工作器 - 队列处理器。 如果没有一个活跃的工作器在运行来处理你的队列,那么任务(TestEvent)将“保留”在作业表中,这意味着任务处于挂起状态,并且在一个活跃的工作器开始处理队列之前不会发生任何进一步的事情。

      你可以像这样启动工作器:

    2. www@yourmachine# PHP artisan queue:work --tries=3
      
    3. 现在您已经准备好在“http://your-app.laravel/broadcast”上进行调用,并检查Pusher调试控制台是否有响应。

    可选步骤: 如果仍然有遗漏,您可以像这样调试与Pusher的应用程序交互: 在您的测试路由中尝试执行以下操作:

    Route::get('/broadcast', function() {
        
        // New Pusher instance with our config data
        $pusher = new \Pusher(
            config('broadcasting.connections.pusher.key'),
            config('broadcasting.connections.pusher.secret'),
            config('broadcasting.connections.pusher.app_id'),
            config('broadcasting.connections.pusher.options')
        );
            
        // Enable pusher logging - I used an anonymous class and the Monolog
        $pusher->set_logger(new class {
               public function log($msg)
               {
                     \Log::info($msg);
               }
        });
            
        // Your data that you would like to send to Pusher
        $data = ['text' => 'hello world from Laravel 5.3'];
            
        // Sending the data to channel: "test_channel" with "my_event" event
        $pusher->trigger( 'test_channel', 'my_event', $data);
            
        return 'ok'; 
    });
    

    我希望这对你也管用!祝你编程愉快! ;)


    3

    1/ 你应该清除缓存

    php artisan config:cache
    
    php artisan config:clear
    
    php artisan route:cache
    
    php artisan route:clear
    
    php artisan optimize --force
    

    2/ 并尝试以下推送消息的方法

    2.1/ 包含:use Pusher\Laravel\Facades\Pusher;

    2.2/ 在路由中添加:

    Pusher::trigger('adevChannel', 'EventAdev', ['message' => $message]);
    

    或者

       event(EventAdev($message) );
    

    php artisan optimize 部分非常有用。 - Wailan Tirajoh

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接