robier/

php-pubsub-http

php-pubsub 包的 HTTP 适配器

1.0.1-beta 2022-11-20 17:15 UTC

This package is auto-updated.

Last update: 2024-09-20 21:12:07 UTC


README

这是 php-pubsub 包的 HTTP 适配器。

Author Build Status StyleCI Software License Packagist Version Total Downloads

该适配器假设您有一个 HTTP 服务,该服务接受 POST 到 /messages/(channel) 端点的消息数组。

一个服务器端实现 js-pubsub-rest-proxy,可以作为即插即用的 Docker 硬件提供。

安装

composer require superbalist/php-pubsub-http

用法

putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/../your-gcloud-key.json');

// create the underlying adapter which is going to be decorated
$pubSubClient = new \Google\Cloud\PubSub\PubSubClient([
    'projectId' => 'your-project-id-here',
]);

$subscribeAdapter = new \Superbalist\PubSub\GoogleCloud\GoogleCloudPubSubAdapter($pubSubClient);

// now create our decorator
// the decorator will proxy subscribe calls straight to the $subscribeAdapter
// publish calls will be POSTed to the service uri
$client = new \GuzzleHttp\Client();

$adapter = new \Superbalist\PubSub\HTTP\HTTPPubSubAdapter($client, 'https://127.0.0.1', $subscribeAdapter);

// consume messages
$adapter->subscribe('my_channel', function ($message) {
    var_dump($message);
});

// publish messages
$adapter->publish('my_channel', 'HELLO WORLD');
$adapter->publish('my_channel', ['hello' => 'world']);

// publish multiple messages
$messages = [
    'Hello World!',
    ['hello' => 'world'],
];
$adapter->publishBatch('my_channel', $messages);

示例

该库包含适配器的 示例 和运行示例脚本的 Dockerfile

运行 make up

您将开始于 /opt/php-pubsub 目录下的 bash 提示符。

如果您需要另一个 shell 向阻塞消费者发布消息,您可以运行 docker-compose run php-pubsub-http /bin/bash

要运行示例

$ ./run_consumer.sh
$ ./run_publisher.sh (in a separate shell)