Skip to content

Latest commit

 

History

History
80 lines (53 loc) · 2.12 KB

gps.md

File metadata and controls

80 lines (53 loc) · 2.12 KB

Google Pub Sub transport

A transport for Google Pub Sub cloud MQ. It uses internally official google sdk library google/cloud-pubsub

Installation

$ composer require enqueue/gps

Create context

To enable the Google Cloud Pub/Sub Emulator, set the PUBSUB_EMULATOR_HOST environment variable. There is a handy docker container google/cloud-sdk.

<?php
use Enqueue\Gps\GpsConnectionFactory;

putenv('PUBSUB_EMULATOR_HOST=http://localhost:8900');

$connectionFactory = new GpsConnectionFactory();

// save as above 
$connectionFactory = new GpsConnectionFactory('gps:');

$psrContext = $connectionFactory->createContext();

// if you have enqueue/enqueue library installed you can use a factory to build context from DSN 
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('gps:')->createContext();

Send message to topic

Before you can send message you have to declare a topic. The operation creates a topic on a broker side. Google allows messages to be sent only to topic.

<?php
/** @var \Enqueue\Gps\GpsContext $psrContext */

$fooTopic = $psrContext->createTopic('foo');
$message = $psrContext->createMessage('Hello world!');

$psrContext->declareTopic($fooTopic);

$psrContext->createProducer()->send($fooTopic, $message);

Consume message:

Before you can consume message you have to subscribe a queue to the topic. Google does not allow consuming message from the topic directly.

<?php
/** @var \Enqueue\Gps\GpsContext $psrContext */

$fooTopic = $psrContext->createTopic('foo');
$fooQueue = $psrContext->createQueue('foo');

$psrContext->subscribe($fooTopic, $fooQueue);

$consumer = $psrContext->createConsumer($fooQueue);
$message = $consumer->receive();

// process a message

$consumer->acknowledge($message);
// $consumer->reject($message);

back to index