Cloud PubSub Client - Class Message (2.6.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
Name Description
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
Type Description
string

attribute

Retrieve a single message attribute.

Example:

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

The attribute key

Returns
Type Description
string|null

attributes

Retrieve all message attributes.

Example:

$attributes = $message->attributes();
Returns
Type Description
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
Type Description
string

orderingKey

Get the message ordering key.

Example:

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

publishTime

Get the message published time.

Example:

$time = $message->publishTime();
Returns
Type Description
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
Type Description
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
Type Description
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
Type Description
Google\Cloud\PubSub\Subscription

info

Get the message data.

Available keys are ackId, subscription and message.

Example:

$info = $message->info();
Returns
Type Description
array

toArray

Get the message as an array.

Returns
Type Description
array