superbalist / php-pubsub
此包已被弃用且不再维护。未建议替代包。
基于PubSub模式的PHP适配器包
2.0.0
2017-05-16 09:52 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: ^5.5
This package is auto-updated.
Last update: 2024-08-12 00:20:29 UTC
README
PHP的pub-sub模式抽象
安装
composer require superbalist/php-pubsub
适配器
- 本地(捆绑的)
- /dev/null(捆绑的)
- Redis - https://github.com/Superbalist/php-pubsub-redis
- Kafka - https://github.com/Superbalist/php-pubsub-kafka
- Google Cloud - https://github.com/Superbalist/php-pubsub-google-cloud
- HTTP - https://github.com/Superbalist/php-pubsub-http
集成
想要快速开始?查看以下集成
用法
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter(); // consume messages $adapter->subscribe('my_channel', function ($message) { var_dump($message); }); // publish messages $adapter->publish('my_channel', 'Hello World!'); // publish multiple messages $messages = [ 'message 1', 'message 2', ]; $adapter->publishBatch('my_channel', $messages);
编写适配器
您可以通过实现PubSubAdapterInterface接口轻松编写自己的自定义适配器。
您的适配器必须实现以下方法
/** * Subscribe a handler to a channel. * * @param string $channel * @param callable $handler */ public function subscribe($channel, callable $handler); /** * Publish a message to a channel. * * @param string $channel * @param mixed $message */ public function publish($channel, $message); /** * Publish multiple messages to a channel. * * @param string $channel * @param array $messages */ public function publishBatch($channel, array $messages);
示例
该库为所有适配器提供了示例,并提供了Dockerfile以运行示例脚本。
运行make up
。
您将开始在/opt/php-pubsub
目录中的bash
提示符下。
如果您需要另一个shell来向阻塞消费者发布消息,可以运行docker-compose run php-pubsub /bin/bash
运行示例
$ php examples/LocalExample.php