dkx/google-pubsub-subscriber

This package is abandoned and no longer maintained. No replacement package was suggested.

Symfony console command for Google PubSub

2.2.0 2019-12-18 17:26 UTC

This package is auto-updated.

Last update: 2024-01-19 00:53:32 UTC


README

Symfony console command for Google PubSub

Installation

$ composer require dkx/google-pubsub-subscriber

Usage

<?php

use DKX\GooglePubSubSubscriber\SubscriberCommand;
use DKX\GooglePubSubSubscriber\SubscriptionInterface;
use DKX\GooglePubSubSubscriber\SubscriptionsManager;
use Google\Cloud\PubSub\PubSubClient;
use Symfony\Component\Console\Application;

final class TestSubscription implements SubscriptionInterface
{
    public function getConsoleName() : string
    {
        return 'my-test';    
    }

    public function processMessage(array $data) : bool
    {
        var_dump($data);
        return true;
    }
}

$client = new PubSubClient();

$manager = new SubscriptionsManager();
$manager->addSubscription(new TestSubscription());

$command = new SubscriberCommand($client, $manager);

$application = new Application();
$application->add($command);
$application->run();

and run:

$ php index.php pubsub:subscribe my-test my-app.prod.test --max-seconds 500 --max-messages 100
  • my-test: string returned from your subscription's getConsoleName() method
  • my-app.prod.test: name of existing subscription in Google Cloud Pub/Sub

Standalone usage

<?php

use DKX\GooglePubSubSubscriber\Subscriber;
use Google\Cloud\PubSub\PubSubClient;

$client = new PubSubClient();

$subscriber = new Subscriber($client, 'my-app.prod.test');
$subscriber->setMaxSeconds(500);
$subscriber->setMaxMessages(100);

$subscriber->subscribe(function (array $data): bool {
    var_dump($data);
    return true;
});