centrifugal/phpcent
Composer 安装命令:
composer require centrifugal/phpcent
包简介
PHP library to communicate with Centrifugo HTTP API
README 文档
README
PHP library to communicate with Centrifugo v5 HTTP API (for Centrifugo v4 use phpcent v5.x, for Centrifugo v3 use phpcent v4.x).
Library is published on the Composer: https://packagist.org/packages/centrifugal/phpcent
composer require centrifugal/phpcent:~6.0
See Centrifugo server API documentation.
Basic Usage:
$client = new \phpcent\Client("http://localhost:8000/api"); $client->setApiKey("Centrifugo API key"); $client->publish("channel", ["message" => "Hello World"]);
You can use phpcent to create connection token (JWT):
$token = $client->setSecret("Centrifugo secret key")->generateConnectionToken($userId);
Connection token that will be valid for 5 minutes:
$token = $client->setSecret("Centrifugo secret key")->generateConnectionToken($userId, time() + 5*60);
It's also possible to generate channel subscription token:
$token = $client->setSecret("Centrifugo secret key")->generateSubscriptionToken($userId, $channel);
It's also possible to generate channel subscription token with expiration time, for example token for 30 mins:
$token = $client->setSecret("Centrifugo secret key")->generateSubscriptionToken($userId, $channel, time() + 30*60);
Also API key and secret can be set in constructor:
$client = new \phpcent\Client("http://localhost:8000/api", "<API key>", "<secret key>");
Timeouts:
$client->setConnectTimeoutOption(0); // Seconds | 0 = never $client->setTimeoutOption(2); // Seconds
All available API methods:
$response = $client->publish($channel, $data); $response = $client->broadcast($channels, $data); $response = $client->subscribe($channel, $userId); $response = $client->unsubscribe($channel, $userId); $response = $client->disconnect($userId); $response = $client->presence($channel); $response = $client->presenceStats($channel); $response = $client->history($channel); $response = $client->historyRemove($channel); $response = $client->channels(); $response = $client->info(); $response = $client->batch($data);
To use assoc option while decoding JSON in response:
$client->setUseAssoc(true);
SSL
In case if your Centrifugo server has invalid SSL certificate, you can use:
$client->setSafety(false);
You can also use self signed certificate in safe manner:
$client = new \phpcent\Client("https://localhost:8000/api"); $client->setCert("/path/to/certificate.pem"); $client->setCAPath("/ca/path"); // if you need.
Note: Certificate must match with host name in Client address (localhost in example above).
DNS Resolution
This error may indicate your system is having trouble resolving IPv6 addresses:
cURL error: Resolving timed out after [value] milliseconds
By default, both IPv4 and IPv6 addresses will attempt to be resolved. You can force it to only resolve IPv4 addresses with:
$client->forceIpResolveV4();
Testing
Requirements:
The provided PHPUnit tests assume that a local Centrifugo server is running and available at port 8000. This can be accomplished using Docker and the official Centrifugo image.
# Install package dependencies. $ composer install # The following command starts a Centrifugo server running in a background Docker container. $ docker run -d -p 8000:8000 --name centrifugo centrifugo/centrifugo centrifugo --api_insecure # Run the test suite. $ vendor/bin/phpunit # Shut down the Centrifugo container. $ docker stop centrifugo
Authors
centrifugal/phpcent 适用场景与选型建议
centrifugal/phpcent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.48M 次下载、GitHub Stars 达 185, 最近一次更新时间为 2019 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「websocket」 「messaging」 「realtime」 「centrifugo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 centrifugal/phpcent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 centrifugal/phpcent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 centrifugal/phpcent 相关的其它包
同方向 / 同关键字的高下载量 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.
PHP SDK for Mobile Message SMS API
Azure service bus Transport
Official PHP client for NATS messaging system
A Pusher bridge for Laravel 5+
WebSocket view engine
统计信息
- 总下载量: 2.48M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 185
- 点击次数: 16
- 依赖项目数: 8
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-14