PHP Class Google\Cloud\PubSub\Subscription

Example: Create subscription through a topic use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder(); $pubsub = $cloud->pubsub(); $topic = $pubsub->topic('my-topic-name'); $subscription = $topic->subscription('my-new-subscription'); Create subscription through PubSubClient use Google\Cloud\PubSub\PubSubClient; $pubsub = new PubSubClient([ 'projectId' => 'my-awesome-project' ]); $subscription = $pubsub->subscription( 'my-new-subscription', 'my-topic-name' );
Inheritance: use trait ResourceNameTrait
Datei anzeigen Open project: GoogleCloudPlatform/gcloud-php Class Usage Examples

Protected Properties

Property Type Description
$connection Google\Cloud\PubSub\Connection\ConnectionInterface

Public Methods

Method Description
__construct ( Google\Cloud\PubSub\Connection\ConnectionInterface $connection, string $projectId, string $name, string $topicName, boolean $encode, array $info = null ) Create a Subscription.
__debugInfo ( ) : array Present a nicer debug result to people using php 5.6 or greater.
acknowledge ( Message $message, array $options = [] ) : void Acknowledge receipt of a message.
acknowledgeBatch ( array $messages, array $options = [] ) : void Acknowledge receipt of multiple messages at once.
create ( array $options = [] ) : array Execute a service request creating the subscription.
delete ( array $options = [] ) : void Delete a subscription
exists ( array $options = [] ) : boolean Check if a subscription exists.
iam ( ) : Iam Manage the IAM policy for the current Subscription.
info ( array $options = [] ) : array Get info on a subscription
modifyAckDeadline ( Message $message, integer $seconds, array $options = [] ) : void Set the acknowledge deadline for a single ackId.
modifyAckDeadlineBatch ( array $messages, integer $seconds, array $options = [] ) : void Set the acknowledge deadline for multiple ackIds.
modifyPushConfig ( array $pushConfig, array $options = [] ) : void Set the push config for the subscription
name ( ) : string Get the subscription name
pull ( array $options = [] ) : Generator Retrieve new messages from the topic.
reload ( array $options = [] ) : array Retrieve info on a subscription from the API.

Private Methods

Method Description
getMessageAckIds ( array $messages ) : array Get a list of ackIds from a list of Message objects.

Method Details

__construct() public method

The idiomatic way to use this class is through the PubSubClient or Topic, but you can instantiate it directly as well.
public __construct ( Google\Cloud\PubSub\Connection\ConnectionInterface $connection, string $projectId, string $name, string $topicName, boolean $encode, array $info = null )
$connection Google\Cloud\PubSub\Connection\ConnectionInterface The service connection object
$projectId string The current project
$name string The subscription name
$topicName string The topic name the subscription is attached to
$encode boolean Whether messages are encrypted or not.
$info array [optional] Subscription info. Used to pre-populate the object.

__debugInfo() public method

Present a nicer debug result to people using php 5.6 or greater.
public __debugInfo ( ) : array
return array

acknowledge() public method

Use {@see \Google\Cloud\PubSub\Subscription::acknowledgeBatch()} to acknowledge multiple messages at once. Example: $messages = $subscription->pull(); $messagesArray = iterator_to_array($messages); $subscription->acknowledge($messagesArray[0]);
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/acknowledge Acknowledge Message
public acknowledge ( Message $message, array $options = [] ) : void
$message Message A message object.
$options array [optional] Configuration Options
return void

acknowledgeBatch() public method

Use {@see \Google\Cloud\PubSub\Subscription::acknowledge()} to acknowledge a single message. Example: $messages = $subscription->pull(); $messagesArray = iterator_to_array($messages); $subscription->acknowledgeBatch($messagesArray);
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/acknowledge Acknowledge Message
public acknowledgeBatch ( array $messages, array $options = [] ) : void
$messages array An array of messages
$options array Configuration Options
return void

create() public method

The suggested way of creating a subscription is by calling through {@see \Google\Cloud\PubSub\Topic::subscribe()} or {@see \Google\Cloud\PubSub\Topic::subscription()}. Returns subscription info in the format detailed in the documentation for a subscription. **NOTE: Some methods of instantiation of a Subscription do not supply a topic name. The topic name is required to create a subscription.** Example: $topic = $pubsub->topic('my-new-topic'); $subscription = $topic->subscription('my-new-subscription'); $result = $subscription->create();
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/create Create Subscription
public create ( array $options = [] ) : array
$options array [optional] { Configuration Options @type int $ackDeadlineSeconds This value is the maximum time after a subscriber receives a message before the subscriber should acknowledge the message. **Defaults to** `10`. @type array $pushConfig See {@see \Google\Cloud\PubSub\Subscription::modifyPushConfig()} or [PushConfig](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions#PushConfig) for usage. }
return array An array of subscription info

delete() public method

Example: $subscription->delete();
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/delete Delete Subscription
public delete ( array $options = [] ) : void
$options array [optional] Configuration Options.
return void

exists() public method

Service errors will NOT bubble up from this method. It will always return a boolean value. If you want to check for errors, use {@see \Google\Cloud\PubSub\Subscription::info()}. If you need to re-check the existence of a subscription that is already downloaded, call {@see \Google\Cloud\PubSub\Subscription::reload()} first to refresh the cached information. Example: if ($subscription->exists()) { echo 'Subscription exists!'; }
public exists ( array $options = [] ) : boolean
$options array [optional] Configuration Options
return boolean

iam() public method

Example: $iam = $subscription->iam();
See also: https://cloud.google.com/pubsub/access_control PubSub Access Control Documentation
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/getIamPolicy Get Subscription IAM Policy
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/setIamPolicy Set Subscription IAM Policy
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/testIamPermissions Test Subscription Permissions
public iam ( ) : Iam
return Google\Cloud\Iam\Iam

info() public method

If the info is already cached on the object, it will return that result. To fetch a fresh result, use {@see \Google\Cloud\PubSub\Subscription::reload()}. Example: $info = $subscription->info(); echo $info['name']; // projects/my-awesome-project/subscriptions/my-new-subscription
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/get Get Subscription
public info ( array $options = [] ) : array
$options array [optional] Configuration Options
return array Subscription data

modifyAckDeadline() public method

Use {@see \Google\Cloud\PubSub\Subscription::modifyAckDeadlineBatch()} to modify the ack deadline for multiple messages at once. Example: $messages = $subscription->pull(); foreach ($messages as $message) { $subscription->modifyAckDeadline($message, 3); sleep(2); Now we'll acknowledge! $subscription->acknowledge($message); break; }
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/modifyAckDeadline Modify Ack Deadline
public modifyAckDeadline ( Message $message, integer $seconds, array $options = [] ) : void
$message Message A message object
$seconds integer The new ack deadline with respect to the time this request was sent to the Pub/Sub system. Must be >= 0. For example, if the value is 10, the new ack deadline will expire 10 seconds after the ModifyAckDeadline call was made. Specifying zero may immediately make the message available for another pull request.
$options array [optional] Configuration Options
return void

modifyAckDeadlineBatch() public method

Use {@see \Google\Cloud\PubSub\Subscription::modifyAckDeadline()} to modify the ack deadline for a single message. Example: $messages = $subscription->pull(); $messagesArray = iterator_to_array($messages); Set the ack deadline to a minute and a half from now for every message $subscription->modifyAckDeadlineBatch($messagesArray, 3); Delay execution, or make a sandwich or something. sleep(2); Now we'll acknowledge $subscription->acknowledge($messagesArray);
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/modifyAckDeadline Modify Ack Deadline
public modifyAckDeadlineBatch ( array $messages, integer $seconds, array $options = [] ) : void
$messages array An array of messages
$seconds integer The new ack deadline with respect to the time this request was sent to the Pub/Sub system. Must be >= 0. For example, if the value is 10, the new ack deadline will expire 10 seconds after the ModifyAckDeadline call was made. Specifying zero may immediately make the message available for another pull request.
$options array [optional] Configuration Options
return void

modifyPushConfig() public method

Example: $subscription->modifyPushConfig([ 'pushEndpoint' => 'https://www.example.com/foo/bar' ]);
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/modifyPushConfig Modify Push Config
public modifyPushConfig ( array $pushConfig, array $options = [] ) : void
$pushConfig array { Push delivery configuration. See [PushConfig](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions#PushConfig) for more details. @type string $pushEndpoint A URL locating the endpoint to which messages should be pushed. For example, a Webhook endpoint might use "https://example.com/push". @type array $attributes Endpoint configuration attributes. }
$options array [optional] Configuration Options
return void

name() public method

Example: echo $subscription->name();
public name ( ) : string
return string

pull() public method

Example: $messages = $subscription->pull(); foreach ($messages as $message) { echo $message->data(); }
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/pull Pull Subscriptions
public pull ( array $options = [] ) : Generator
$options array [optional] { Configuration Options @type bool $returnImmediately If set, the system will respond immediately, even if no messages are available. Otherwise, wait until new messages are available. @type int $maxMessages Limit the amount of messages pulled. }
return Generator

reload() public method

To use the previously cached result (if it exists), use {@see \Subscription::info()}. Example: $subscription->reload(); $info = $subscription->info(); echo $info['name']; // projects/my-awesome-project/subscriptions/my-new-subscription
See also: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/get Get Subscription
public reload ( array $options = [] ) : array
$options array [optional] Configuration Options
return array Subscription data

Property Details

$connection protected_oe property

protected ConnectionInterface,Google\Cloud\PubSub\Connection $connection
return Google\Cloud\PubSub\Connection\ConnectionInterface