uzdevid/yii-runner-centrifugo
Composer 安装命令:
composer require uzdevid/yii-runner-centrifugo
包简介
Centrifugo & RoadRunner based Yii runner
README 文档
README
Yii RoadRunner Centrifugo
The package is designed to process web sockets on the basis of Centrifugo and Roadrunner
Requirements
- PHP 8.1 or higher.
Installation
The package could be installed with Composer:
composer require uzdevid/yii-runner-centrifugo
General usage
Centrifugo
Download binary file
Generate Centrifugo config file: config.json
./centrifugo genconfig
Example config
{
"log": {
"level": "debug"
},
"client": {
"token": {
"hmac_secret_key": "<secret>"
},
"allowed_origins": [
"*"
],
"proxy": {
"connect": {
"enabled": true,
"endpoint": "grpc://127.0.0.1:30000",
"timeout": "10s"
},
"refresh": {
"enabled": true,
"endpoint": "grpc://127.0.0.1:30000",
"timeout": "3s"
}
}
},
"channel": {
"namespaces": [
{
"name": "<channel>",
"allow_subscribe_for_client": true,
"allow_publish_for_client": true,
"subscribe_proxy_enabled": true,
"publish_proxy_enabled": true
}
],
"proxy": {
"subscribe": {
"endpoint": "grpc://127.0.0.1:30000",
"timeout": "3s"
},
"publish": {
"endpoint": "grpc://127.0.0.1:30000",
"timeout": "3s"
},
"sub_refresh": {
"endpoint": "grpc://127.0.0.1:30000",
"timeout": "3s"
}
}
},
"admin": {
"enabled": true,
"password": "<password>",
"secret": "<secret>"
},
"grpc_api": {
"address": "127.0.0.1",
"port": 30000
}
}
We set up the proxy for all requests for our Roadrunner server at
grpc://127.0.0.1:30000, according to the gRPC protocol
See also: https://centrifugal.dev/docs/server/proxy
RoadRunner
Download roadrunner binary file
Road runner example config: .rr.yaml
version: '3' rpc: listen: tcp://127.0.0.1:6001 server: command: "php ./worker.php" relay: pipes centrifuge: proxy_address: tcp://127.0.0.1:30000 grpc_api_address: tcp://127.0.0.1:30000 use_compressor: true service: centrifuge: service_name_in_log: true remain_after_exit: true restart_sec: 1 command: "./centrifugo"
We listen to the proxies in the address
tcp://127.0.0.1:30000from the Centrifugo server
Runner file
Create /worker.php file
use RoadRunner\Centrifugo\Request\RequestFactory; use Spiral\RoadRunner\Worker; use UzDevid\Yii\Runner\Centrifugo\CentrifugoApplicationRunner; use Yiisoft\ErrorHandler\ErrorHandler; use Yiisoft\ErrorHandler\Renderer\PlainTextRenderer; use Yiisoft\Log\Logger; use Yiisoft\Log\Target\File\FileTarget; ini_set('display_errors', 'stderr'); require_once dirname(__DIR__) . '/vendor/autoload.php'; $logger = new Logger([new FileTarget(sprintf('%s/runtime/logs/centrifugo.log', __DIR__))]); $errorHandler = new ErrorHandler($logger, new PlainTextRenderer()); $worker = Worker::create(logger: $logger); $application = new CentrifugoApplicationRunner( rootPath: __DIR__, temporaryErrorHandler: $errorHandler, worker: $worker, requestFactory: new RequestFactory($worker), debug: true ); $application->run();
Handlers
Handling connect request and response with specific payload
use UzDevid\Yii\Runner\Centrifugo\Handler\ConnectHandlerInterface; use RoadRunner\Centrifugo\Payload\ConnectResponse; class ConnectHandler implements ConnectHandlerInterface { public function handle(RequestInterface $request): void { // ... $request->respond(new ConnectResponse('user-id', null, ['ok' => true])) } }
In the same way, implement the rest of the handlers:
ConnectHandlerInterface- called when a client connects to Centrifugo, so it's possible to authenticate user, return custom initial data to a client, subscribe connection to server-side channels, attach meta information to the connection, and so on. This proxy hook available for both bidirectional and unidirectional transports.RefreshHandlerInterface- called when a client session is going to expire, so it's possible to prolong it or just let it expire. Can also be used as a periodical connection liveness callback from Centrifugo to the app backend. Works for bidirectional and unidirectional transports.SubscribeHandlerInterface- called when clients try to subscribe on a channel, so it's possible to check permissions and return custom initial subscription data. Works for bidirectional transports only.PublishHandlerInterface- called when a client tries to publish into a channel, so it's possible to check permissions and optionally modify publication data. Works for bidirectional transports only.SubRefreshHandlerInterface- called when a client subscription is going to expire, so it's possible to prolong it or just let it expire. Can also be used just as a periodical subscription liveness callback from Centrifugo to app backend. Works for bidirectional and unidirectional transports.InvalidRequestInterface- Handle invalid requestRpcHandlerInterface- called when a client sends RPC, you can do whatever logic you need based on a client-provided RPC method and data. Works for bidirectional transports only (and bidirectional emulation), since data is sent from client to the server in this case.
Run RoadRunner
./rr serve
You can test the WebSocketing service or use the Client SDK
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
Maintained by UzDevid.
Follow updates
uzdevid/yii-runner-centrifugo 适用场景与选型建议
uzdevid/yii-runner-centrifugo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「centrifugo」 「yii3」 「roadrunner-centrifugo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 uzdevid/yii-runner-centrifugo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 uzdevid/yii-runner-centrifugo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 uzdevid/yii-runner-centrifugo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Centrifugo broadcaster for laravel 9 and Centrifugo 4
Centrifugo (Centrifuge) [1.0+] PHP Server REDIS & HTTP API implementation for Laravel 5+
Asset for Bulma Free for YiiFramework v.3.0.
Centrifuge broadcaster for laravel >= 5.3, support Centrifuge v2
PHP library to communicate with Centrifugo HTTP API
Centrifuge broadcaster for laravel >= 5.3, support Centrifuge v2
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-13