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 benull.
↳ 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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud PubSub Client - Class Message (2.13.2)\n\nVersion latestkeyboard_arrow_down\n\n- [2.13.2 (latest)](/php/docs/reference/cloud-pubsub/latest/Message)\n- [2.13.1](/php/docs/reference/cloud-pubsub/2.13.1/Message)\n- [2.12.0](/php/docs/reference/cloud-pubsub/2.12.0/Message)\n- [2.11.3](/php/docs/reference/cloud-pubsub/2.11.3/Message)\n- [2.10.1](/php/docs/reference/cloud-pubsub/2.10.1/Message)\n- [2.9.1](/php/docs/reference/cloud-pubsub/2.9.1/Message)\n- [2.8.2](/php/docs/reference/cloud-pubsub/2.8.2/Message)\n- [2.7.0](/php/docs/reference/cloud-pubsub/2.7.0/Message)\n- [2.6.0](/php/docs/reference/cloud-pubsub/2.6.0/Message)\n- [2.5.2](/php/docs/reference/cloud-pubsub/2.5.2/Message)\n- [2.4.0](/php/docs/reference/cloud-pubsub/2.4.0/Message)\n- [2.3.0](/php/docs/reference/cloud-pubsub/2.3.0/Message)\n- [2.2.1](/php/docs/reference/cloud-pubsub/2.2.1/Message)\n- [2.1.2](/php/docs/reference/cloud-pubsub/2.1.2/Message)\n- [1.50.0](/php/docs/reference/cloud-pubsub/1.50.0/Message)\n- [1.49.0](/php/docs/reference/cloud-pubsub/1.49.0/Message)\n- [1.48.0](/php/docs/reference/cloud-pubsub/1.48.0/Message)\n- [1.47.0](/php/docs/reference/cloud-pubsub/1.47.0/Message)\n- [1.46.5](/php/docs/reference/cloud-pubsub/1.46.5/Message)\n- [1.45.2](/php/docs/reference/cloud-pubsub/1.45.2/Message)\n- [1.44.0](/php/docs/reference/cloud-pubsub/1.44.0/Message)\n- [1.43.2](/php/docs/reference/cloud-pubsub/1.43.2/Message)\n- [1.42.1](/php/docs/reference/cloud-pubsub/1.42.1/Message)\n- [1.41.3](/php/docs/reference/cloud-pubsub/1.41.3/Message)\n- [1.40.1](/php/docs/reference/cloud-pubsub/1.40.1/Message)\n- [1.39.3](/php/docs/reference/cloud-pubsub/1.39.3/Message) \nReference documentation and code samples for the Cloud PubSub Client class Message.\n\nRepresents a PubSub Message.\n\nExample: \n\n use Google\\Cloud\\PubSub\\PubSubClient;\n\n $pubsub = new PubSubClient();\n $subscription = $pubsub-\u003esubscription('my-new-subscription');\n\n $messages = $subscription-\u003epull();\n foreach ($messages as $message) {\n echo $message-\u003edata();\n }\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ PubSub\n\nMethods\n-------\n\n### __construct\n\n### data\n\nThe message payload.\n\nExample: \n\n echo $message-\u003edata();\n\n### attribute\n\nRetrieve a single message attribute.\n\nExample: \n\n echo $message-\u003eattribute('browser-name');\n\n### attributes\n\nRetrieve all message attributes.\n\nExample: \n\n $attributes = $message-\u003eattributes();\n\n### id\n\nGet the message ID.\n\nThe message ID is assigned by the server when the message is published.\nGuaranteed to be unique within the topic.\n\nExample: \n\n echo $message-\u003eid();\n\n### orderingKey\n\nGet the message ordering key.\n\nExample: \n\n $orderingKey = $message-\u003eorderingKey();\n\n### publishTime\n\nGet the message published time.\n\nExample: \n\n $time = $message-\u003epublishTime();\n\n### ackId\n\nGet the message ackId.\n\nThis is only set when message is obtained via\n[Subscription::pull()](/php/docs/reference/cloud-pubsub/latest/Subscription#_Google_Cloud_PubSub_Subscription__pull__).\n\nExample: \n\n echo $message-\u003eackId();\n\n### deliveryAttempt\n\nGet the delivery attempt count.\n\nIf a DeadLetterPolicy is not set on the subscription, this will be `null`.\n\nExample: \n\n echo $message-\u003edeliveryAttempt();\n\n### subscription\n\nGet the subcription through which the message was obtained.\n\nThis is only set when the message is obtained via push delivery.\n\nExample: \n\n echo \"Subscription Name: \". $message-\u003esubscription()-\u003ename();\n\n### info\n\nGet the message data.\n\nAvailable keys are `ackId`, `subscription` and `message`.\n\nExample: \n\n $info = $message-\u003einfo();\n\n### toArray\n\nGet the message as an array."]]