fanout / laravel-fanout
1.2.0
2018-11-28 16:48 UTC
Requires
- php: >=7.0.0
- fanout/fanout: ^2.0.0
Requires (Dev)
- phpunit/phpunit: 3.7.14
This package is not auto-updated.
Last update: 2021-08-31 00:39:20 UTC
README
作者:Konstantin Bokarius kon@fanout.io
Fanout.io库用于Laravel。
鸣谢
本项目基于Christopher Thomas的以下拉取请求: https://github.com/cwt137/laravel-framework/commit/b64b57ec0dfac975db9fadecac9f7a3757b7af30
要求
- openssl
- curl
- pthreads(异步发布所需)
- php >=7.0.0
- laravel >= 5.5
- fanout/fanout >=2.0.0(通过Composer自动检索)
安装
使用Composer
composer require fanout/laravel-fanout
手动:确保已包含php-fanout,并在laravel-fanout中需要以下文件
require 'laravel-fanout/src/fanoutbroadcaster.php'; require 'laravel-fanout/src/fanoutbroadcastserviceprovider.php';
异步发布
为了进行异步发布调用,必须安装pthreads。如果未安装pthreads,则只能进行同步发布调用。要安装pthreads,请使用以下标志重新编译PHP: '--enable-maintainer-zts'
请注意,由于传递给publish_async方法的回调将在单独的线程中执行,因此该回调及其所属的类将受到pthreads扩展施加的规则和限制。
有关pthreads的更多信息,请参阅: https://php.ac.cn/manual/en/book.pthreads.php
用法
在您的config/broadcasting.php文件中,将默认驱动程序设置为'fanout',并添加如下配置
'default' => 'fanout', 'connections' => [ ... 'fanout' => [ 'driver' => 'fanout', 'realm_id' => '<my-realm-id>', 'realm_key' => '<my-realm-key>', 'ssl' => true, 'publish_async' => false ], ... ]
在您的config/app.php文件中,将以下提供者添加到您的服务提供者数组中
'providers' => [ ... LaravelFanout\FanoutBroadcastServiceProvider::class, ... ]
按如下方式向您的应用程序添加自定义广播事件
namespace App\Events; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class PublishToFanoutEvent implements ShouldBroadcast { use SerializesModels; public $message; public function __construct($message) { $this->message = $message; } public function broadcastOn() { return ['<channel>']; } }
现在,要向Fanout.io发布,只需触发事件即可
event(new App\Events\PublishToFanoutEvent('Test publish!!'));