Cloud PubSub Client - Class Topic (2.4.0)

Reference documentation and code samples for the Cloud PubSub Client class Topic.

A named resource to which messages are sent by publishers.

Example:

use Google\Cloud\PubSub\PubSubClient;

$pubsub = new PubSubClient(['projectId' => 'my-awesome-project']);
$topic = $pubsub->topic('my-new-topic');
// You can also pass a fully-qualified topic name:
$topic = $pubsub->topic('projects/my-awesome-project/topics/my-new-topic');

Namespace

Google \ Cloud \ PubSub

Methods

__construct

Create a PubSub topic.

Parameters
Name Description
requestHandler Google\Cloud\Core\RequestHandler
serializer Google\ApiCore\Serializer

The serializer instance to encode/decode messages.

projectId string

The project Id

name string

The topic name

encode bool

Whether messages should be base64 encoded.

info array

A [Topic](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics

↳ name string

The name of the topic.

↳ labels array

Key value pairs used to organize your resources.

↳ kmsKeyName string

The resource name of the Cloud KMS CryptoKey to be used to protect access to messages published on this topic. The expected format is projects/my-project/locations/kr-location/keyRings/my-kr/cryptoKeys/my-key.

↳ enableCompression bool

Flag to enable compression of messages before publishing. Set the flag to true to enable compression. Defaults to false. Messsages are compressed if their total size >= compressionBytesThreshold, whose default value has been experimentally derived after performance evaluations.

↳ compressionBytesThreshold int

The threshold byte size above which messages are compressed. This only takes effect if enableCompression is set to true. Defaults to 240. (This value is experiementally derived after performance evaluations.)

clientConfig array

[optional] Configuration options for the PubSub client used to handle processing of batch items through the daemon. For valid options please see Google\Cloud\PubSub\PubSubClient::__construct(). Defaults to the options provided to the PubSub client associated with this instance.

name

Get the topic name

Example:

echo $topic->name();
Returns
Type Description
string

create

See also:

Parameters
Name Description
options array

Configuration Options

↳ labels array

Key value pairs used to organize your resources.

↳ kmsKeyName string

The resource name of the Cloud KMS CryptoKey to be used to protect access to messages published on this topic. The expected format is projects/my-project/locations/kr-location/keyRings/my-kr/cryptoKeys/my-key.

↳ messageStoragePolicy array

Policy constraining the set of Google Cloud Platform regions where messages published to the topic may be stored. If not present, then no constraints are in effect.

↳ messageStoragePolicy string[]

.allowedPersistenceRegions A list of IDs of GCP regions where messages that are published to the topic may be persisted in storage. Messages published by publishers running in non-allowed GCP regions (or running outside of GCP altogether) will be routed for storage in one of the allowed regions. An empty list means that no regions are allowed, and is not a valid configuration.

↳ schemaSettings string|Schema

.schema The name of a schema that messages published should be validated against, or an instance of Google\Cloud\PubSub\Schema.

↳ schemaSettings string

.encoding The encoding of messages validated against schema. For allowed values, see constants defined on Google\Cloud\PubSub\V1\Encoding}. </xref

Returns
Type Description
array Topic information

update

See also:

Parameters
Name Description
data array

The Topic data.

↳ labels array

Key value pairs used to organize your resources.

↳ messageStoragePolicy array

Policy constraining the set of Google Cloud Platform regions where messages published to the topic may be stored. If not present, then no constraints are in effect.

↳ messageStoragePolicy string[]

.allowedPersistenceRegions A list of IDs of GCP regions where messages that are published to the topic may be persisted in storage. Messages published by publishers running in non-allowed GCP regions (or running outside of GCP altogether) will be routed for storage in one of the allowed regions. An empty list means that no regions are allowed, and is not a valid configuration.

↳ schemaSettings string|Schema

.schema The name of a schema that messages published should be validated against, or an instance of Google\Cloud\PubSub\Schema.

↳ schemaSettings string

.encoding The encoding of messages validated against schema. For allowed values, see constants defined on Google\Cloud\PubSub\V1\Encoding}. </xref

options array

Configuration options.

↳ updateMask array

A list of field paths to be modified. Nested key names should be dot-separated, e.g. messageStoragePolicy.allowedPersistenceRegions. Google Cloud PHP will attempt to infer this value on your behalf, however modification of map fields with arbitrary keys (such as labels or message storage policy) requires an explicit update mask.

Returns
Type Description
array The topic info.

delete

See also:

Parameter
Name Description
options array

[optional] Configuration Options

Returns
Type Description
void

exists

Check if a topic exists.

Service errors will NOT bubble up from this method. It will always return a boolean value. If you want to check for errors, use Google\Cloud\PubSub\Topic::info().

Example:

if ($topic->exists()) {
    echo 'Topic exists';
}
Parameter
Name Description
options array

[optional] Configuration Options

Returns
Type Description
bool

info

See also:

Parameter
Name Description
options array

[optional] Configuration Options

Returns
Type Description
array [A representation of a Topic](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics)

reload

See also:

Parameter
Name Description
options array

[optional] Configuration Options

Returns
Type Description
array [A representation of a Topic](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics)

publish

See also:

Parameters
Name Description
message Google\Cloud\PubSub\Message|array

An instance of Google\Cloud\PubSub\Message, or an array in the correct Message Format.

options array

[optional] Configuration Options

Returns
Type Description
array A list of message IDs

publishBatch

See also:

Parameters
Name Description
messages array<Google\Cloud\PubSub\Message>|array[]

A list of messages. Each message must be in the correct Message Format, or be an instance of Google\Cloud\PubSub\Message.

options array

[optional] Configuration Options

Returns
Type Description
array A list of message IDs.

batchPublisher

Push a message into a batch queue, to be processed at a later point.

Example:

$topic->batchPublisher()
    ->publish([
        'data' => 'New User Registered',
        'attributes' => [
            'id' => '2',
            'userName' => 'Dave',
            'location' => 'Detroit'
        ]
    ]);
Parameters
Name Description
options array

Configuration options.

↳ debugOutput bool

Whether or not to output debug information. Please note debug output currently only applies in CLI based applications. Defaults to false.

↳ debugOutputResource resource

A resource to output debug output to. Defaults to Resource for php://stderr.

↳ batchOptions array

A set of options for a BatchJob. Google\Cloud\Core\Batch\BatchJob::__construct() for more details. Defaults to ['batchSize' => 1000, 'callPeriod' => 2.0, 'numWorkers' => 2].

↳ clientConfig array

Configuration options for the PubSub client used to handle processing of batch items. For valid options please see Google\Cloud\PubSub\PubSubClient::__construct(). Defaults to the options provided to the client associated with the current Topic instance.

↳ batchRunner BatchRunner

A BatchRunner object. Mainly used for the tests to inject a mock. Defaults to a newly created BatchRunner.

↳ identifier string

An identifier for the batch job. Defaults to pubsub-topic-{topic-name}. Example: pubsub-topic-mytopic.

↳ closureSerializer ClosureSerializerInterface

An implementation responsible for serializing closures used in the $clientConfig. This is especially important when using the batch daemon. Defaults to Google\Cloud\Core\Batch\OpisClosureSerializer if the opis/closure library is installed.

↳ enableCompression bool

Flag to enable compression of messages before publishing. Set the flag to true to enable compression. Defaults to false. Messsages are compressed if their total size >= compressionBytesThreshold, whose default value has been experimentally derived after performance evaluations.

↳ compressionBytesThreshold int

The threshold byte size above which messages are compressed. This only takes effect if enableCompression is set to true. Defaults to 240. (This value is experiementally derived after performance evaluations.)

Returns
Type Description
Google\Cloud\PubSub\BatchPublisher

subscribe

Parameters
Name Description
name string

The subscription name

options array

[optional] Please see Google\Cloud\PubSub\Subscription::create() for configuration details.

Returns
Type Description
Google\Cloud\PubSub\Subscription

subscription

This method will not run any API requests. You will receive a Subscription object that you can use to interact with the API.

Example:

$subscription = $topic->subscription('my-new-subscription');
Parameter
Name Description
name string

The subscription name

Returns
Type Description
Google\Cloud\PubSub\Subscription

subscriptions

Parameters
Name Description
options array

Configuration Options

↳ pageSize int

Maximum number of subscriptions to return.

↳ resultLimit int

Limit the number of results returned in total. Defaults to 0 (return all results).

↳ pageToken string

A previously-returned page token used to resume the loading of results from a specific point.

Returns
Type Description
Google\Cloud\Core\Iterator\ItemIterator<\google\cloud\pubsub\subscription>

iam

Returns
Type Description
Google\Cloud\Core\Iam\IamManager

__debugInfo

Present a nicer debug result to people using php 5.6 or greater.

Returns
Type Description
array

Constants

DEFAULT_COMPRESSION_BYTES_THRESHOLD

Value: 240

DEFAULT_ENABLE_COMPRESSION

Value: false