wunderwerkio/emitter-sdk
Composer 安装命令:
composer require wunderwerkio/emitter-sdk
包简介
PHP SDK for the emitter event broker
README 文档
README
This repository provides a PHP SDK for Emitter.
Installation
Using composer:
composer require wunderwerkio/emitter-sdk
Emitter API
This PHP SDK implements the following methods as described in the emitter docs:
-
connect -
disconnect -
publish -
subscribe -
unsubscribe -
keygen -
presence -
me
Examples
Connect to emitter server
use Wunderwerk\EmitterSDK\Emitter; // Create Emitter instance. $emitter = new Emitter(); // Connect to emitter server at host `localhost` and port `8080`. // A username can be optionally passed as the third argument. // The username is only used for the presence features (see https://emitter.io/develop/presence/). $emitter->connect('localhost', 8080, 'my-username'); // Disconnect from server. $emitter->disconnect();
Publish
$emitter->publish( key: '__channel-key-with-write-permission__', channel: 'article1/', message: 'Hello World!' );
Subscribe
Please note that this method simply subscribes for a channel.
To receive messages, the addMessageHandler method must be used!
$emitter->subscribe( key: '__channel-key-with-read-permission__', channel: 'article1/' );
Listen for messages
In order to receive incoming messages from the server, a callback handler must be registered with addMessageHandler.
Additionally, a event loop must be started by calling loop() to start listening for server connections.
This loop method is blocking, but can be released by calling the interrupt() method.
To avoid a deadlock, make sure you structure your code so that the interrupt() method is called once the desired messages are received.
use Wunderwerk\EmitterSDK\EmitterInterface; // Listen for incoming messages. // Note: Multiple handlers can be assigned! $handler = function (EmitterInterface $emitter, string $message, string $topic): void { printf('Incoming message for topic %s: %s', $topic, $message); // Interrupt event loop once the first message is received. $emitter->interrupt(); }; $emitter->addMessageHandler($handler); // A handler can be removed again. $emitter->removeMessageHandler($handler); // Start event loop. // The TRUE here means, that the event loop sleeps for a short amount of time before checking again for new messages. $emitter->loop(TRUE);
Loop handler
The internal event loop supports calling custom handlers on each loop cycle. This can be used to implement timeouts for example to interrupt the event loop if no messages are received in 10 seconds.
use Wunderwerk\EmitterSDK\EmitterInterface; $handler = function (EmitterInstance $emitter, float $elapsedTime): void { printf('Loop running for %d seconds', $elapsedTime); // Interrupt event loop after 10 seconds runtime. if ($elapsedTime >= 10) { $emitter->interrupt(); } } // Register handler. $emitter->addLoopHandler($handler); // Handler can be removed again. $emitter->removeLoopHandler($handler);
Generate a channel key
To publish and subscribe to channels, emitter needs a channel key.
A channel key can be generated with the keygen() method by passing the master key, the channel name, the desired permissions and optionally a TTL in seconds for the channel key.
$channelKey = $emitter->keygen( key: '__master-key-from-emitter-server__', channel: 'article1/', type: 'rwp' // Give permissions for e.g. r(ead), w(write) and p(resence) ttl: 0 // TTL in seconds for how long this key should be valid. 0 means key is valid indefinitely. );
Device presence
Emitter has built-in support for device presence.
To receive presence data from other users, the presence() method can be used to subscribe to presence events.
If the changes argument is FALSE changes in presence of other users will not be automatically received and have to be requested via presence() again.
Please note, that emitter only sends presence info for a maximum of 1000 users.
$emitter->presence( channelKey: '_channel-key-with-presence-permission__', channel: 'article1/', status: TRUE, // Receive full status in response. changes: TRUE, // Subscribe to presence changes. )
wunderwerkio/emitter-sdk 适用场景与选型建议
wunderwerkio/emitter-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.75k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wunderwerkio/emitter-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wunderwerkio/emitter-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-16