Cloud PubSub Client - Class Message (1.50.0)

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

Represents a PubSub Message.

Example:

use Google\Cloud\PubSub\PubSubClient;

$pubsub = new PubSubClient();
$subscription = $pubsub->subscription('my-new-subscription');

$messages = $subscription->pull();
foreach ($messages as $message) {
    echo $message->data();
}

Namespace

Google \ Cloud \ PubSub

Methods

__construct

Parameters
NameDescription
message array

Message Options

See [PubsubMessage](https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage).
↳ data string

The message data field. If this is empty, the message must contain at least one attribute.

↳ attributes array

Optional attributes for this message.

↳ messageId string

ID of this message, assigned by the server when the message is published.

↳ publishTime string

The time at which the message was published, populated by the server when it receives the publish call.

↳ orderingKey string

The message ordering key.

metadata array

Message metadata

↳ ackId string

The message ackId. This is only set when messages are pulled from the PubSub service.

↳ deliveryAttempt int

Delivery attempt counter is 1 + (the sum of number of NACKs and number of ack_deadline exceeds) for this message. If a DeadLetterPolicy is not set on the subscription, this will be null.

↳ subscription Subscription

The subscription the message was obtained from. This is only set when messages are delivered by pushDelivery

data

The message payload.

Example:

echo $message->data();
Returns
TypeDescription
string

attribute

Retrieve a single message attribute.

Example:

echo $message->attribute('browser-name');
Parameter
NameDescription
key string

The attribute key

Returns
TypeDescription
string|null

attributes

Retrieve all message attributes.

Example:

$attributes = $message->attributes();
Returns
TypeDescription
array

id

Get the message ID.

The message ID is assigned by the server when the message is published. Guaranteed to be unique within the topic.

Example:

echo $message->id();
Returns
TypeDescription
string

orderingKey

Get the message ordering key.

Example:

$orderingKey = $message->orderingKey();
Returns
TypeDescription
string|null

publishTime

Get the message published time.

Example:

$time = $message->publishTime();
Returns
TypeDescription
DateTimeInterface|null

ackId

Get the message ackId.

This is only set when message is obtained via Google\Cloud\PubSub\Subscription::pull().

Example:

echo $message->ackId();
Returns
TypeDescription
string

deliveryAttempt

Get the delivery attempt count.

If a DeadLetterPolicy is not set on the subscription, this will be null.

Example:

echo $message->deliveryAttempt();
Returns
TypeDescription
int|null

subscription

Get the subcription through which the message was obtained.

This is only set when the message is obtained via push delivery.

Example:

echo "Subscription Name: ". $message->subscription()->name();
Returns
TypeDescription
Google\Cloud\PubSub\Subscription

info

Get the message data.

Available keys are ackId, subscription and message.

Example:

$info = $message->info();
Returns
TypeDescription
array

toArray

Get the message as an array.

Returns
TypeDescription
array