oleh-ozimok/php-centrifugo
Composer 安装命令:
composer require oleh-ozimok/php-centrifugo
包简介
PHP client for Centrifugo real-time messaging server
关键字:
README 文档
README
PHP client for Centrifugo real-time messaging server
Features
- Support publishing messages via Redis engine API listener (publish, broadcast, unsubscribe, disconnect methods only)
- Support client chain (Redis -> HTTP -> ...) as failover. If Redis down or method not supported by Redis engine API, client try send message via HTTP
- Support batch requests
- Support Predis
Quick Examples
Create Centrifugo client
<?php use Centrifugo\Centrifugo; use Centrifugo\Clients\HttpClient; $centrifugo = new Centrifugo('http://example.com/api/', 'secret api key', new HttpClient());
Create Centrifugo client with Redis API support
<?php use Centrifugo\Centrifugo; use Centrifugo\Clients\RedisClient; use Centrifugo\Clients\HttpClient; use Centrifugo\Clients\Redis\RedisTransport; use Centrifugo\Clients\Redis\PredisTransport; // Create Redis transport $redis = new \Redis(); $redis->connect('localhost'); $redisTransport = new RedisTransport($redis); // Or Predis transport $predis = new Predis\Client(['host' => 'localhost']); $redisTransport = new PredisTransport($predis); // Create Centrifugo RedisClient $centrifugoRedisClient = new RedisClient($redisTransport); $centrifugoRedisClient->setShardsNumber(12); // Add Centrifugo HttpClient as failover $centrifugoHttpClient = new HttpClient(); $centrifugoRedisClient->setFailover($centrifugoHttpClient); $centrifugo = new Centrifugo('http://example.com/api/', 'secret api key', $centrifugoRedisClient);
Send request to Centrifugo
<?php use Centrifugo\Centrifugo; use Centrifugo\Exceptions\CentrifugoException; $userId = 1; $channel = '#chan_1'; $messageData = ['message' => 'Hello, world!']; try { //Send message into channel. $response = $centrifugo->publish($channel, $messageData); //Very similar to publish but allows to send the same data into many channels. $response = $centrifugo->broadcast([$channel], $messageData); //Unsubscribe user from channel. $response = $centrifugo->unsubscribe($channel, $userId); //Disconnect user by user ID. $response = $centrifugo->disconnect($userId); //Get channel presence information (all clients currently subscribed on this channel). $response = $centrifugo->presence($channel); //Get channel history information (list of last messages sent into channel). $response = $centrifugo->history($channel); //Get channels information (list of currently active channels). $response = $centrifugo->channels(); //Get stats information about running server nodes. $response = $centrifugo->stats(); //Get information about single Centrifugo node. $response = $centrifugo->node('http://node1.example.com/api/'); } catch (CentrifugoException $e) { // invalid response }
Send batch request
<?php use Centrifugo\Centrifugo; use Centrifugo\Exceptions\CentrifugoException; $userId = '1'; //must be a string $channel = '#chan_1'; $messageData = ['message' => 'Hello, world!']; try { $requests[] = $centrifugo->request('publish', ['channel' => $channel, 'data' => $messageData]); $requests[] = $centrifugo->request('broadcast', ['channel' => $channel, 'data' => $messageData]); $requests[] = $centrifugo->request('unsubscribe', ['channel' => $channel, 'user' => $userId]); $requests[] = $centrifugo->request('disconnect', ['user' => $userId]); $batchResponse = $centrifugo->sendBatchRequest($requests); foreach ($batchResponse as $response) { if ($response->isError()) { // get error info $error = $response->getError(); } else { // get response data as array $responseData = $response->getDecodedBody(); } } } catch (CentrifugoException $e) { // invalid response }
Related Projects
centrifugo-bundle (under development)
oleh-ozimok/php-centrifugo 适用场景与选型建议
oleh-ozimok/php-centrifugo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 515.56k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2016 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「real-time」 「centrifugo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 oleh-ozimok/php-centrifugo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oleh-ozimok/php-centrifugo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oleh-ozimok/php-centrifugo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Talk is a Laravel based realtime messaging, chatting and conversation system. It helps to develop users messaging, chatting and conversations in easy way.
Centrifugo broadcaster for laravel 9 and Centrifugo 4
Boxalino Real Time User Experience (RTUX) Magento2 plugin
Centrifugo (Centrifuge) [1.0+] PHP Server REDIS & HTTP API implementation for Laravel 5+
Centrifuge broadcaster for laravel >= 5.3, support Centrifuge v2
PHP library to communicate with Centrifugo HTTP API
统计信息
- 总下载量: 515.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 39
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-20