superbalist/php-pubsub-http

此包已被废弃,不再维护。未建议替代包。

php-pubsub包的HTTP适配器

1.0.0 2017-05-24 06:47 UTC

This package is auto-updated.

Last update: 2024-08-12 00:13:12 UTC


README

php-pubsub包的php-pubsub适配器。

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)