batformat/phpcent-broadcaster
Composer 安装命令:
composer require batformat/phpcent-broadcaster
包简介
Centrifuge broadcaster for laravel >= 5.3, support Centrifuge v2
README 文档
README
Introduction
Inspired by laracomponents/centrifuge-broadcaster
Centrifuge broadcaster for laravel >= 5.3
Requirements
- PHP 5.6.4+ or newer
- Laravel 5.3 or newer
- Centrifugo Server 2.0 or newer (see here)
If you use Centrifugo Server 1.6 you should use LaraComponents/centrifuge-broadcaster package
Installation
Require this package with composer:
composer require batFormat/phpcent-broadcaster
Open your config/app.php and add the following to the providers array:
'providers' => [ // ... TheArdent\Centrifuge\CentrifugeServiceProvider::class, // And uncomment BroadcastServiceProvider App\Providers\BroadcastServiceProvider::class, ],
Open your config/broadcasting.php and add the following to it:
'connections' => [ 'centrifuge' => [ 'driver' => 'centrifuge', 'api_key' => env('CENTRIFUGE_API_KEY'), // you api key 'secret' => env('CENTRIFUGE_SECRET'), // you secret key 'url' => env('CENTRIFUGE_URL', 'http://localhost:8000'), // centrifuge api url 'redis_api' => env('CENTRIFUGE_REDIS_API', false), // enable or disable Redis API 'redis_connection' => env('CENTRIFUGE_REDIS_CONNECTION', 'default'), // name of redis connection 'redis_prefix' => env('CENTRIFUGE_REDIS_PREFIX', 'centrifugo'), // prefix name for queue in Redis 'redis_num_shards' => env('CENTRIFUGE_REDIS_NUM_SHARDS', 0), // number of shards for redis API queue 'verify' => env('CENTRIFUGE_VERIFY', false), // Verify host ssl if centrifuge uses this 'ssl_key' => env('CENTRIFUGE_SSL_KEY', null), // Self-Signed SSl Key for Host (require verify=true) ], // ... ],
For the redis configuration, add a new connection in config/database.php
'redis' => [ 'centrifuge' => [ 'host' => env('REDIS_HOST', '127.0.0.1'),, 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, ], // ... ],
You can also add a configuration to your .env file:
CENTRIFUGE_API_KEY=very-long-api-key
CENTRIFUGE_SECRET=very-long-secret-key
CENTRIFUGE_URL=http://localhost:8000
CENTRIFUGE_REDIS_API=false
CENTRIFUGE_REDIS_CONNECTION=centrifuge
CENTRIFUGE_REDIS_PREFIX=centrifugo
CENTRIFUGE_REDIS_NUM_SHARDS=0
CENTRIFUGE_SSL_KEY=/etc/ssl/some.pem
CENTRIFUGE_VERIFY=false
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 of using the client:
<?php namespace App\Http\Controllers; use TheArdent\Centrifuge\Centrifuge; class ExampleController extends Controller { public function home(Centrifuge $centrifuge) { // Send message into channel: $centrifuge->publish('channel-name', [ 'key' => 'value' ]); // Generate token without expire: $token = $centrifuge->generateToken('user id'); // Connection token that will be valid for 5 minutes: $token = $centrifuge->generateConnectionToken('user id', time() + 5*60); //It's also possible to generate private channel subscription token: $token = $centrifuge->generatePrivateChannelToken('user id', 'channel'); } }
Available methods
| Name | Description |
|---|---|
| publish(string $channel, array $data) | Send message into channel. |
| broadcast(array $channels, array $data) | Send message into multiple channel. |
| presence(string $channel) | Get channel presence information (all clients currently subscribed on this channel). |
| history(string $channel) | Get channel history information (list of last messages sent into channel). |
| unsubscribe(string $user_id, string $channel) | Unsubscribe user from channel. |
| disconnect(string $user_id) | Disconnect user by its ID. |
| channels() | Get channels information (list of currently active channels). |
| stats() | Get stats information about running server nodes. |
| generateToken(string $userOrClient, int $exp, array $info = []) | Generate token with expire time. |
| generatePrivateChannelToken($userOrClient, $channel, $exp = 0, $info = []) | Generate private channel toekn. |
Difference with LaraComponents/centrifuge-broadcaster
| Name | Description |
|---|---|
| publish | Doesn't have client parameter. |
| broadcast | Doesn't have client parameter. |
| generateToken | Generate connection token(JWT),have new parameter int $exp = 0. |
| generateApiSign | Deprecated. |
| generatePrivateChannelToken | New method generate private channel subscription token. |
License
The MIT License (MIT). Please see License File for more information.
batformat/phpcent-broadcaster 适用场景与选型建议
batformat/phpcent-broadcaster 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.19k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「centrifuge」 「centrifugo」 「broadcaster」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 batformat/phpcent-broadcaster 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 batformat/phpcent-broadcaster 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 batformat/phpcent-broadcaster 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Centrifuge broadcaster for laravel ^6
Centrifuge broadcaster for laravel >= 5.3
Centrifugo broadcaster for laravel 9 and Centrifugo 4
Centrifugo (Centrifuge) [1.0+] PHP Server REDIS & HTTP API implementation for Laravel 5+
PHP library to communicate with Centrifugo HTTP API
Centrifuge broadcaster for laravel >= 5.3, support Centrifuge v2
统计信息
- 总下载量: 3.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-13