Coder Social home page Coder Social logo

isabella232 / php-pubsub-google-cloud Goto Github PK

View Code? Open in Web Editor NEW

This project forked from superbalist/php-pubsub-google-cloud

0.0 0.0 0.0 58 KB

A Google Cloud adapter for the php-pubsub package

License: MIT License

Makefile 0.99% PHP 94.16% Dockerfile 4.85%

php-pubsub-google-cloud's Introduction

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.