Reference documentation and code samples for the Cloud PubSub Client class MessageBuilder.
Builds a PubSub Message.
This class may be used to build an API-compliant message for publication.
This class is immutable and each build method returns a new instance with
your changes applied.
Note that messages are invalid unless they include a data field or at least
one attribute. Both may be provided, but omission of both will result in an
error.
Example:
use Google\Cloud\PubSub\MessageBuilder;
use Google\Cloud\PubSub\PubSubClient;
$client = new PubSubClient();
$topic = $client->topic($topicId);
$builder = new MessageBuilder();
$builder = $builder->setData('hello friend!')
->addAttribute('from', 'Bob')
->addAttribute('to', 'Jane');
$topic->publish($builder->build());
Namespace
Google \ Cloud \ PubSub
Methods
__construct
Parameter
Name
Description
message
array
The initial message data.
setData
Set the message data.
Do not base64-encode the value; If it must be encoded, the client will
do it on your behalf.
[[["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 MessageBuilder (2.13.2)\n\nVersion latestkeyboard_arrow_down\n\n- [2.13.2 (latest)](/php/docs/reference/cloud-pubsub/latest/MessageBuilder)\n- [2.13.1](/php/docs/reference/cloud-pubsub/2.13.1/MessageBuilder)\n- [2.12.0](/php/docs/reference/cloud-pubsub/2.12.0/MessageBuilder)\n- [2.11.3](/php/docs/reference/cloud-pubsub/2.11.3/MessageBuilder)\n- [2.10.1](/php/docs/reference/cloud-pubsub/2.10.1/MessageBuilder)\n- [2.9.1](/php/docs/reference/cloud-pubsub/2.9.1/MessageBuilder)\n- [2.8.2](/php/docs/reference/cloud-pubsub/2.8.2/MessageBuilder)\n- [2.7.0](/php/docs/reference/cloud-pubsub/2.7.0/MessageBuilder)\n- [2.6.0](/php/docs/reference/cloud-pubsub/2.6.0/MessageBuilder)\n- [2.5.2](/php/docs/reference/cloud-pubsub/2.5.2/MessageBuilder)\n- [2.4.0](/php/docs/reference/cloud-pubsub/2.4.0/MessageBuilder)\n- [2.3.0](/php/docs/reference/cloud-pubsub/2.3.0/MessageBuilder)\n- [2.2.1](/php/docs/reference/cloud-pubsub/2.2.1/MessageBuilder)\n- [2.1.2](/php/docs/reference/cloud-pubsub/2.1.2/MessageBuilder)\n- [1.50.0](/php/docs/reference/cloud-pubsub/1.50.0/MessageBuilder)\n- [1.49.0](/php/docs/reference/cloud-pubsub/1.49.0/MessageBuilder)\n- [1.48.0](/php/docs/reference/cloud-pubsub/1.48.0/MessageBuilder)\n- [1.47.0](/php/docs/reference/cloud-pubsub/1.47.0/MessageBuilder)\n- [1.46.5](/php/docs/reference/cloud-pubsub/1.46.5/MessageBuilder)\n- [1.45.2](/php/docs/reference/cloud-pubsub/1.45.2/MessageBuilder)\n- [1.44.0](/php/docs/reference/cloud-pubsub/1.44.0/MessageBuilder)\n- [1.43.2](/php/docs/reference/cloud-pubsub/1.43.2/MessageBuilder)\n- [1.42.1](/php/docs/reference/cloud-pubsub/1.42.1/MessageBuilder)\n- [1.41.3](/php/docs/reference/cloud-pubsub/1.41.3/MessageBuilder)\n- [1.40.1](/php/docs/reference/cloud-pubsub/1.40.1/MessageBuilder)\n- [1.39.3](/php/docs/reference/cloud-pubsub/1.39.3/MessageBuilder) \nReference documentation and code samples for the Cloud PubSub Client class MessageBuilder.\n\nBuilds a PubSub Message.\n\nThis class may be used to build an API-compliant message for publication.\n\nThis class is immutable and each build method returns a new instance with\nyour changes applied.\n\nNote that messages are invalid unless they include a data field or at least\none attribute. Both may be provided, but omission of both will result in an\nerror.\n\nExample: \n\n use Google\\Cloud\\PubSub\\MessageBuilder;\n use Google\\Cloud\\PubSub\\PubSubClient;\n\n $client = new PubSubClient();\n $topic = $client-\u003etopic($topicId);\n\n $builder = new MessageBuilder();\n $builder = $builder-\u003esetData('hello friend!')\n -\u003eaddAttribute('from', 'Bob')\n -\u003eaddAttribute('to', 'Jane');\n\n $topic-\u003epublish($builder-\u003ebuild());\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ PubSub\n\nMethods\n-------\n\n### __construct\n\n### setData\n\nSet the message data.\n\nDo not base64-encode the value; If it must be encoded, the client will\ndo it on your behalf.\n\nExample: \n\n $builder = $builder-\u003esetData('Hello friend!');\n\n### setAttributes\n\nSet optional attributes for this message.\n\nExample: \n\n $builder = $builder-\u003esetAttributes([\n 'from' =\u003e 'Bob'\n ]);\n\n### addAttribute\n\nAdd a single attribute to the message.\n\nExample: \n\n $builder = $builder-\u003eaddAttribute('to', 'Jane');\n\n### setOrderingKey\n\nSet the message's ordering key.\n\nExample: \n\n $builder = $builder-\u003esetOrderingKey('order');\n\n### build\n\nBuild a message.\n\nExample: \n\n $message = $builder-\u003ebuild();"]]