iliakologrivov/centrifuge
Composer 安装命令:
composer require iliakologrivov/centrifuge
包简介
centrifuge broadcaster for laravel
README 文档
README
Introduction
Centrifuge broadcaster for laravel >= 5.4
Requirements
- PHP 7.1.3+ or newer
- Laravel 5.4 or newer
- Centrifugo Server 2.0 or newer (see here)
Installation
Require this package with composer:
composer require iliakologrivov/centrifuge
./artisan vendor:publish --provider='IliaKologrivov\Centrifuge\CentrifugeServiceProvider'
Open your web routes and add:
IliaKologrivov\Centrifuge\CentrifugeRouterFacade::subscribe(); IliaKologrivov\Centrifuge\CentrifugeRouterFacade::refresh();
Open your config/broadcasting.php and add the following to it:
'connections' => [ 'centrifuge' => [ 'driver' => 'centrifuge', ], // ... ],
You can also add a configuration to your .env file:
CENTRIFUGE_SECRET=<very-long-secret-key>
CENTRIFUGE_API_KEY=<secret-key-for-centrifuge-api>
CENTRIFUGE_CONNECTION_URL='ws://localhost:8000/connection/websocket
CENTRIFUGE_URL=http://localhost:8000
CENTRIFUGE_VERIFY=false
CENTRIFUGE_SSL_KEY=/etc/ssl/some.pem
CENTRIFUGE_BROADCAST_ERROR=true
Do not forget to install the broadcast driver
BROADCAST_DRIVER=centrifuge
Basic Usage
To configure the Centrifugo server, read the official documentation
For broadcasting events, see the official documentation of laravel
A simple example:
<?php namespace App\Events; use Illuminate\Broadcasting\InteractsWithSockets; use IliaKologrivov\Centrifuge\Channels\Channel; use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; class event implements ShouldBroadcastNow { use Dispatchable, InteractsWithSockets, SerializesModels; public function broadcastAs() { return 'event-name'; } /** * Get the channels the event should broadcast on. * * @return \Centrifuge\Channels\Channel|array */ public function broadcastOn() { return new Channel('example-channel'); } }
Use Blade directive in your views
@centrifugeConnectToken(<custom_user_token>)//get connect token for custom id user @centrifugeAuthConnectToken(86600)//get connect user for auth user @centrifugeWsConnectUrl()//get connect url for ws
Example view
<script> const centrifugeUserToken = '@centrifugeAuthConnectToken(86600)'; const centrifugeConnectionUrl = '@centrifugeWsConnectUrl'; </script> <script src="{!! mix('/js/app.js') !!}"></script>
Added in bootstrap.js
window.Centrifuge = require('centrifuge'); window.centrifuge = new Centrifuge(centrifugeConnectionUrl); centrifuge.setToken(centrifugeUserToken); centrifuge.connect(); centrifuge.on('connect', function(context) { console.log('connect', context); }).on('disconnect', function(context) { console.error('disconnect', context); centrifuge.disconnect(); }).on('error', function(error) { console.error(error); });
License
The MIT License (MIT). Please see License File for more information.
iliakologrivov/centrifuge 适用场景与选型建议
iliakologrivov/centrifuge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 iliakologrivov/centrifuge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iliakologrivov/centrifuge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-20