This document shows you how to publish messages to a topic with a schema.
Before you begin
Before configuring the publish workflow, ensure you have completed the following tasks:
- Understand how Pub/Sub schemas work.
- Associate a schema with a topic.
Required roles
To get the permissions that you need to publish messages to a topic,
ask your administrator to grant you the
Pub/Sub Publisher (roles/pubsub.publisher
) IAM role on the topic.
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
You need additional permissions to create or update topics and subscriptions.Publish messages with schema
You can publish messages to a topic that is associated with a schema. You must encode the messages in the schema and format that you specified when you created the topic. A message matches the schema associated with the topic if it matches any of the schema's revision in the allowed range of revisions. Messages are evaluated against revisions in order from the most recent allowed revision until either a match is found or the oldest allowed revision is reached. Pub/Sub adds the following attributes to a message successfully published to a topic associated with a schema:
googclient_schemaname
: The name of the schema used for validation.googclient_schemaencoding
: The encoding of the message, either JSON or BINARY.googclient_schemarevisionid
: The revision ID of the schema used to parse and validate the message. Each revision has a unique revision ID associated with it. The revision ID is an auto-generated eight-character UUID.
When a message does not match any of the schema revisions allowed by
the topic, Pub/Sub returns an
INVALID_ARGUMENT
error to the publish request.
Pub/Sub only evaluates messages against schema revisions at publish time. Committing a new new schema revision or changing the schema associated with a topic after publishing a message does not re-evaluate that message nor change any of the attached schema message attributes.
You can publish messages to a topic with an associated schema in a Google Cloud project using the Google Cloud console, the gcloud CLI, the Pub/Sub API, or the Cloud Client Libraries.
gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Publish a sample message using the gcloud pubsub topics publish command.
gcloud pubsub topics publish TOPIC_ID \ --message=MESSAGE
Replace the following:
TOPIC_ID: Name of the topic that you already created.
MESSAGE: Message published to the topic. A sample message can be
{"name": "Alaska", "post_abbr": "AK"}
.
C++
Before trying this sample, follow the C++ setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub C++ API reference documentation.
Avro ProtoC#
Before trying this sample, follow the C# setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub C# API reference documentation.
Avro ProtoGo
Before trying this sample, follow the Go setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Go API reference documentation.
Avro ProtoJava
Before trying this sample, follow the Java setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Java API reference documentation.
Avro ProtoNode.js
Before trying this sample, follow the Node.js setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Node.js API reference documentation.
Avro Protocol BufferNode.js
Before trying this sample, follow the Node.js setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Node.js API reference documentation.
Avro Protocol BufferPHP
Before trying this sample, follow the PHP setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub PHP API reference documentation.
Avro Protocol BufferPython
Before trying this sample, follow the Python setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Python API reference documentation.
Avro Protocol BufferRuby
Before trying this sample, follow the Ruby setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Ruby API reference documentation.
Avro Protocol BufferWhat's next
To restrict the locations in which Pub/Sub stores message data, see Restricting Pub/Sub resource locations.
To learn more about receiving messages, see Choose a subscription type.