Reference documentation and code samples for the Cloud PubSub Client class PubSubClient.
Google Cloud Pub/Sub allows you to send and receive messages between independent applications. Find more information at the Google Cloud Pub/Sub docs.
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:
$ pecl install grpc
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.
When using gRPC in production environments, it is highly recommended that you make use of the Protobuf PHP extension for improved performance. Protobuf can be installed via PECL.
$ pecl install protobuf
Example:
use Google\Cloud\PubSub\PubSubClient;
$pubsub = new PubSubClient(['projectId' => 'my-project']);
// Using the Pub/Sub Emulator
use Google\Cloud\PubSub\PubSubClient;
// Be sure to use the port specified when starting the emulator.
// `8900` is used as an example only.
putenv('PUBSUB_EMULATOR_HOST=localhost:8900');
$pubsub = new PubSubClient(['projectId' => 'my-project']);
Namespace
Google \ Cloud \ PubSubMethods
__construct
Create a PubSub client.
Parameters | |
---|---|
Name | Description |
config |
array
Configuration Options. |
↳ projectId |
string
The project ID from the Google Developer's Console. |
↳ apiEndpoint |
string
The hostname with optional port to use in place of the default service endpoint. Example: |
↳ credentials |
string|array|FetchAuthTokenInterface|CredentialsWrapper
The credentials to be used by the client to authorize API calls. This option accepts either a path to a credentials file, or a decoded credentials file as a PHP array. Advanced usage: In addition, this option can also accept a pre-constructed Google\Auth\FetchAuthTokenInterface object or Google\ApiCore\CredentialsWrapper object. Note that when one of these objects are provided, any settings in $credentialsConfig will be ignored. Important: If you accept a credential configuration (credential JSON/File/Stream) from an external source for authentication to Google Cloud Platform, you must validate it before providing it to any Google API or library. Providing an unvalidated credential configuration to Google APIs can compromise the security of your systems and data. For more information https://cloud.google.com/docs/authentication/external/externally-sourced-credentials |
↳ credentialsConfig |
array
Options used to configure credentials, including auth token caching, for the client. For a full list of supporting configuration options, see Google\ApiCore\CredentialsWrapper::build() . |
↳ transport |
string|TransportInterface
The transport used for executing network requests. May be either the string |
↳ transportConfig |
array
Configuration options that will be used to construct the transport. Options for each supported transport type should be passed in a key for that transport. For example: $transportConfig = [ 'grpc' => [...], 'rest' => [...], ]; See the Google\ApiCore\Transport\GrpcTransport::build() and Google\ApiCore\Transport\RestTransport::build() methods for the supported options. |
createTopic
See also:
Parameters | |
---|---|
Name | Description |
name |
string
The topic name |
options |
array
[optional] Configuration Options. For available
configuration options, refer to
Topic::create() {
@type bool $enableCompression Flag to enable compression of messages
before publishing. Set the flag to |
Returns | |
---|---|
Type | Description |
Topic |
topic
Lazily instantiate a topic with a topic name.
No API requests are made by this method. If you want to create a new topic, use Topic::create().
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`
Parameters | |
---|---|
Name | Description |
name |
string
The topic name |
options |
array
[optional] Configuration Options {
@type bool $enableCompression Flag to enable compression of messages
before publishing. Set the flag to |
Returns | |
---|---|
Type | Description |
Topic |
topics
See also:
Parameters | |
---|---|
Name | Description |
options |
array
Configuration Options |
↳ pageSize |
int
Maximum number of results to return per request. |
↳ resultLimit |
int
Limit the number of results returned in total. Defaults to |
↳ 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<Topic> |
subscribe
See also:
Parameters | |
---|---|
Name | Description |
name |
string
A subscription name |
topic |
Topic|string
The topic to which the new subscription will be subscribed. |
options |
array
[optional] Please see Subscription::create() for configuration details. |
Returns | |
---|---|
Type | Description |
Subscription |
subscription
Lazily instantiate a subscription with a subscription name.
This method will NOT perform any API calls. If you wish to create a new subscription, use PubSubClient::subscribe().
Unless you are sure the subscription exists, you should check its existence before using it.
Example:
$subscription = $pubsub->subscription('my-new-subscription');
Parameters | |
---|---|
Name | Description |
name |
string
The subscription name |
topicName |
string
[optional] The topic name |
Returns | |
---|---|
Type | Description |
Subscription |
subscriptions
See also:
Parameters | |
---|---|
Name | Description |
options |
array
Configuration Options |
↳ pageSize |
int
Maximum number of results to return per request. |
↳ resultLimit |
int
Limit the number of results returned in total. Defaults to |
↳ 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<Subscription> |
createSnapshot
Create a snapshot.
Please note that this method may not yet be available in your project.
Example:
$subscription = $pubsub->subscription($subscriptionName);
$snapshot = $pubsub->createSnapshot('my-snapshot', $subscription);
Parameters | |
---|---|
Name | Description |
name |
string
The snapshot name. |
subscription |
Subscription
The subscription to take a snapshot of. |
options |
array
[optional] Configuration options. |
Returns | |
---|---|
Type | Description |
Snapshot |
snapshot
Lazily create a snapshot instance.
Example:
$snapshot = $pubsub->snapshot('my-snapshot');
Parameters | |
---|---|
Name | Description |
name |
string
The snapshot name. |
info |
array
[optional] Snapshot info. |
Returns | |
---|---|
Type | Description |
Snapshot |
snapshots
Get a list of the snapshots in the project.
Please note that this method may not yet be available in your project.
Example:
$snapshots = $pubsub->snapshots();
foreach ($snapshots as $snapshot) {
$info = $snapshot->info();
echo $info['name'];
}
Parameters | |
---|---|
Name | Description |
options |
array
Configuration Options |
↳ pageSize |
int
Maximum number of results to return per request. |
↳ resultLimit |
int
Limit the number of results returned in total. Defaults to |
↳ 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<Snapshot> |
schema
Lazily instantiate a schema object.
Example:
$schema = $pubsub->schema('my-schema');
Parameters | |
---|---|
Name | Description |
schemaId |
string
The schema ID. Must exist in the current project. |
info |
array
[optional] The schema resource info. |
Returns | |
---|---|
Type | Description |
Schema |
createSchema
See also:
Parameters | |
---|---|
Name | Description |
schemaId |
string
The desired schema ID. |
type |
string
The schema type. Allowed values are |
definition |
string
The definition of the schema. This should
contain a string representing the full definition of the schema that
is a valid schema definition of the type specified in |
options |
array
[optional] Configuration options |
Returns | |
---|---|
Type | Description |
Schema |
schemas
See also:
Parameters | |
---|---|
Name | Description |
options |
array
Configuration Options |
↳ view |
string
The set of Schema fields to return in the response. If not set, returns Schemas with |
↳ pageSize |
int
Maximum number of results to return per request. |
↳ resultLimit |
int
Limit the number of results returned in total. Defaults to |
↳ 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<Schema> |
validateSchema
See also:
Parameters | |
---|---|
Name | Description |
schema |
array
The schema to validate. See Schema for available parameters. |
options |
array
[optional] Configuration options |
Returns | |
---|---|
Type | Description |
void |
validateMessage
See also:
Parameters | |
---|---|
Name | Description |
schema |
Schema|string|array
The schema to validate against. If a
string is given, it should be a fully-qualified schema name, e.g.
|
message |
string
The base64 encoded message to validate. |
encoding |
string
Either |
options |
array
[optional] Configuration options |
Returns | |
---|---|
Type | Description |
void |
consume
Consume an incoming message and return a PubSub Message.
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);
Parameter | |
---|---|
Name | Description |
requestData |
array
|
Returns | |
---|---|
Type | Description |
Message |
timestamp
Create a Timestamp object.
Example:
$timestamp = $pubsub->timestamp(new \DateTime('2003-02-05 11:15:02.421827Z'));
Parameters | |
---|---|
Name | Description |
timestamp |
DateTimeInterface
|
nanoSeconds |
int
[optional] The number of nanoseconds in the timestamp. |
Returns | |
---|---|
Type | Description |
Google\Cloud\Core\Timestamp |
duration
Create a Duration object.
Example:
$duration = $pubsub->duration(100, 00001);
Parameters | |
---|---|
Name | Description |
seconds |
int
The number of seconds in the duration. |
nanos |
int
[optional] The number of nanoseconds in the duration.
Defaults to* |
Returns | |
---|---|
Type | Description |
Google\Cloud\Core\Duration |
__debugInfo
Constants
VERSION
Value: '2.11.3'
FULL_CONTROL_SCOPE
Value: 'https://www.googleapis.com/auth/pubsub'