PHP Class Google\Cloud\PubSub\PubSubClient

To enable the Google Cloud Pub/Sub Emulator, set the PUBSUB_EMULATOR_HOST environment variable. This client supports transport over REST or gRPC. In order to enable gRPC support please make sure to install and enable the gRPC extension through PECL: sh $ pecl install grpc Afterwards, please install the following dependencies through composer: sh $ composer require google/gax && composer require google/proto-client-php Please take care in installing the same version of these libraries that are outlined in the project's composer.json require-dev keyword. NOTE: Support for gRPC is currently at an Alpha quality level, meaning it is still a work in progress and is more likely to get backwards-incompatible updates. Example: use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder(); $pubsub = $cloud->pubsub(); PubSubClient can be instantiated directly. use Google\Cloud\PubSub\PubSubClient; $pubsub = new PubSubClient(); Using the Pub/Sub Emulator use Google\Cloud\ServiceBuilder; Be sure to use the port specified when starting the emulator. 8900 is used as an example only. putenv('PUBSUB_EMULATOR_HOST=http://localhost:8900'); $cloud = new ServiceBuilder(); $pubsub = $cloud->pubsub();
Inheritance: use trait Google\Cloud\ClientTrait, use trait IncomingMessageTrait, use trait ResourceNameTrait
Datei anzeigen Open project: GoogleCloudPlatform/gcloud-php

Protected Properties

Property Type Description
$connection ConnectionInterface

Public Methods

Method Description
__construct ( array $config = [] ) Create a PubSub client.
consume ( array $requestData ) : Message Consume an incoming message and return a PubSub Message.
createTopic ( string $name, array $options = [] ) : Topic Create a topic.
subscribe ( string $name, string $topicName, array $options = [] ) : Subscription Create a Subscription. If the subscription does not exist, it will be created.
subscription ( string $name, string $topicName = null ) : Subscription Lazily instantiate a subscription with a subscription name.
subscriptions ( array $options = [] ) : Generator Get a list of the subscriptions registered to all of your project's topics.
topic ( string $name ) : Topic Lazily instantiate a topic with a topic name.
topics ( array $options = [] ) : Generator Get a list of the topics registered to your project.

Private Methods

Method Description
subscriptionFactory ( string $name, string $topicName = null, array $info = null ) : Subscription Create a subscription instance.
topicFactory ( string $name, array $info = null ) : Topic Create an instance of a topic

Method Details

__construct() public method

Create a PubSub client.
public __construct ( array $config = [] )
$config array [optional] { Configuration Options. @type string $projectId The project ID from the Google Developer's Console. @type CacheItemPoolInterface $authCache A cache for storing access tokens. **Defaults to** a simple in memory implementation. @type array $authCacheOptions Cache configuration options. @type callable $authHttpHandler A handler used to deliver Psr7 requests specifically for authentication. @type callable $httpHandler A handler used to deliver Psr7 requests. Only valid for requests sent over REST. @type string $keyFile The contents of the service account credentials .json file retrieved from the Google Developers Console. @type string $keyFilePath The full path to your service account credentials .json file retrieved from the Google Developers Console. @type int $retries Number of retries for a failed request. **Defaults to** `3`. @type array $scopes Scopes to be used for the request. @type string $transport The transport type used for requests. May be either `grpc` or `rest`. **Defaults to** `grpc` if gRPC support is detected on the system. }

consume() public method

This method is for use with push delivery only. Example: $httpPostRequestBody = file_get_contents('php://input'); $requestData = json_decode($httpPostRequestBody, true); $message = $pubsub->consume($requestData);
public consume ( array $requestData ) : Message
$requestData array
return Message

createTopic() public method

Unlike {@see \Google\Cloud\PubSub\PubSubClient::topic()}, this method will send an API call to create the topic. If the topic already exists, an exception will be thrown. When in doubt, use {@see \Google\Cloud\PubSub\PubSubClient::topic()}. Example: $topic = $pubsub->createTopic('my-new-topic'); echo $topic->info()['name']; // projects/my-awesome-project/topics/my-new-topic
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/create Create Topic
public createTopic ( string $name, array $options = [] ) : Topic
$name string The topic name
$options array [optional] Configuration Options
return Topic

subscribe() public method

Use {@see \Google\Cloud\PubSub\PubSubClient::subscription()} to create a subscription object without any API requests. If the topic already exists, an exception will be thrown. When in doubt, use {@see \Google\Cloud\PubSub\PubSubClient::subscription()}. Example: Create a subscription. If it doesn't exist in the API, it will be created. $subscription = $pubsub->subscribe('my-new-subscription', 'my-topic-name');
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/create Create Subscription
public subscribe ( string $name, string $topicName, array $options = [] ) : Subscription
$name string A subscription name
$topicName string The topic to which the new subscription will be subscribed.
$options array [optional] Please see {@see \Google\Cloud\PubSub\Subscription::create()} for configuration details.
return Subscription

subscription() public method

This method will NOT perform any API calls. If you wish to create a new subscription, use {@see \Google\Cloud\PubSub\PubSubClient::subscribe()}. Unless you are sure the subscription exists, you should check its existence before using it. Example: $subscription = $pubsub->subscription('my-new-subscription');
public subscription ( string $name, string $topicName = null ) : Subscription
$name string The subscription name
$topicName string [optional] The topic name
return Subscription

subscriptions() public method

Example: $subscriptions = $pubsub->subscriptions(); foreach ($subscriptions as $subscription) { $info = $subscription->info(); echo $info['name']; // projects/my-awesome-project/subscriptions/ }
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/list List Subscriptions
public subscriptions ( array $options = [] ) : Generator
$options array [optional] { Configuration Options @type int $pageSize Maximum number of results to return per request. }
return Generator

topic() public method

No API requests are made by this method. If you want to create a new topic, use {@see \Google\Cloud\PubSub\Topic::createTopic()}. Example: No API request yet! $topic = $pubsub->topic('my-new-topic'); This will execute an API call. echo $topic->info()['name']; // projects/my-awesome-project/topics/my-new-topic
public topic ( string $name ) : Topic
$name string The topic name
return Topic

topics() public method

Example: $topics = $pubsub->topics(); foreach ($topics as $topic) { $info = $topic->info(); echo $info['name']; // projects/my-awesome-project/topics/my-new-topic }
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/list List Topics
public topics ( array $options = [] ) : Generator
$options array [optional] { Configuration Options @type int $pageSize Maximum number of results to return per request. }
return Generator

Property Details

$connection protected_oe property

protected ConnectionInterface $connection
return ConnectionInterface