Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Superbalist/php-pubsub-google-cloud

Repository files navigation

php-pubsub-google-cloud

A Google Cloud adapter for the php-pubsub package.

Author Build Status Software License Packagist Version Total Downloads

Installation

composer require superbalist/php-pubsub-google-cloud

Usage

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

$client = new \Google\Cloud\PubSub\PubSubClient([
    'projectId' => 'your-project-id-here',
]);

$adapter = new \Superbalist\PubSub\GoogleCloud\GoogleCloudPubSubAdapter($client);


// disable auto topic & subscription creation
$adapter->setAutoCreateTopics(false); // this is true by default
$adapter->setAutoCreateSubscriptions(false); // this is true by default

// set a unique client identifier for the subscriber
$adapter->setClientIdentifier('search_service');

// consume messages
// note: this is a blocking call
$adapter->subscribe('my_channel', function ($message) {
    var_dump($message);
});

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

gRPC Support

Google Cloud PHP v0.12.0 added support for communication over the gRPC protocol.

gRPC is great for high-performance, low-latency applications, and is highly recommended in cases where performance and latency are concerns.

The library will automatically choose gRPC over REST if all dependencies are installed.

pecl install grpc

composer require google/gax
composer require google/proto-client

Background Batch Message Support

Google Cloud v0.33.0 added support for queueing messages and publishing in the background. This is available in version 5+ of this package which requires a min version of google/cloud ^0.33.0.

You can enable background batch messaging by setting $backgroundBatching to true when constructing the GoogleCloudPubSubAdapter or by calling setBackgroundBatching(true) on an existing adapter.

If the semaphore and pcntl PHP extensions are enabled AND the IS_BATCH_DAEMON_RUNNING ENV var is set to true, the library will queue messages to be published by the Batch Daemon. The Batch Daemon needs to be manually run as a long-lived background process.

For all other cases, messages will be queued in memory and will be published before the script terminates using a vendor registered shutdown handler.

Please Note

This is marked by google/cloud as an experimental feature & may change before release in backwards-incompatible ways.

Examples

The library comes with examples for the adapter and a Dockerfile for running the example scripts.

Run make up.

You will start at a bash prompt in the /opt/php-pubsub directory.

If you need another shell to publish a message to a blocking consumer, you can run docker-compose run php-pubsub-google-cloud /bin/bash

To run the examples:

$ php examples/GoogleCloudConsumerExample.php
$ php examples/GoogleCloudPublishExample.php (in a separate shell)