GSP094
Overview
Pub/Sub lets applications exchange messages reliably, quickly, and asynchronously. A data producer publishes messages to a Pub/Sub topic, and a subscriber client creates a subscription to that topic to consume messages. Pub/Sub reliably persists undelivered messages for up to seven days.
In this lab, you learn how to publish and subscribe to messages with Pub/Sub using the Python client library.
What you'll do
In this lab, you do the following:
- Learn the basics of Pub/Sub.
- Create and list Pub/Sub topics and subscriptions.
- Publish messages to a topic.
- Use a pull subscriber to output individual topic messages.
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
- Time to complete the lab—remember, once you start, you cannot pause a lab.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.
How to start your lab and sign in to the Google Cloud console
-
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method.
On the right is the Lab setup and access panel with the following:
- The Open Google Cloud console button
- The temporary credentials (username and password) that you must use for this lab
- Other information, if needed, to step through this lab
Note that the lab timer is located near the top of the page, showing the remaining time.
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account.
-
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}}
You can also find the Username in the Lab setup and access panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}}
You can also find the Password in the Lab setup and access panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials.
Note: Using your own Google Cloud account for this lab may incur extra charges.
-
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
-
Click Activate Cloud Shell
at the top of the Google Cloud console.
-
Click through the following windows:
- Continue through the Cloud Shell information window.
- Authorize Cloud Shell to use your credentials to make Google Cloud API calls.
When you are connected, you are already authenticated, and the project is set to your Project_ID, . The output contains a line that declares the Project_ID for this session:
Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
gcloud auth list
- Click Authorize.
Output:
ACTIVE: *
ACCOUNT: {{{user_0.username | "ACCOUNT"}}}
To set the active account, run:
$ gcloud config set account `ACCOUNT`
- (Optional) You can list the project ID with this command:
gcloud config list project
Output:
[core]
project = {{{project_0.project_id | "PROJECT_ID"}}}
Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Create a virtual environment
Python virtual environments isolate package installations from the system.
- Build the virtual environment:
python3 -m venv venv
- Activate the virtual environment:
source venv/bin/activate
Task 2. Install the client library
- Run the following command to install the client library:
pip install --upgrade google-cloud-pubsub
- Clone the GitHub repository to download the sample code:
git clone https://github.com/googleapis/python-pubsub.git
- Change to the
snippets directory:
cd python-pubsub/samples/snippets
Task 3. Review Pub/Sub basics
Pub/Sub is an asynchronous global messaging service. Pub/Sub uses three core concepts: topics, publishing, and subscribing.
A topic is a shared string that lets applications connect with one another through a common thread.
Publishers push (or publish) messages to a Pub/Sub topic. Subscribers then create a subscription to that topic and either pull messages from it or configure webhooks for push subscriptions. Every subscriber must acknowledge each message within a configurable window of time.
In summary, a publisher creates and sends messages to a topic, and a subscriber creates a subscription to a topic to receive messages from it.
Pub/Sub in Google Cloud
The gcloud Pub/Sub CLI comes preinstalled in Cloud Shell, so you don't need any additional CLI configuration to get started. In this lab, you use Python to create a topic and a subscription, and then view messages. You use a gcloud command to publish messages to the topic.
Task 4. Create a topic
To publish data to Pub/Sub, create a topic and configure a publisher for it.
- In Cloud Shell, run the following command to verify that the
GOOGLE_CLOUD_PROJECT environment variable stores your project ID:
echo $GOOGLE_CLOUD_PROJECT
-
Verify that the output matches the Project ID in the Lab Details panel.
-
View the contents of publisher.py, a script that demonstrates how to perform basic operations on topics with the Pub/Sub API:
cat publisher.py
Note: Alternatively, use a shell editor preinstalled in Cloud Shell, such as nano or vim, or use Cloud Shell Editor to view python-pubsub/samples/snippets/publisher.py.
- Run the following command to view usage information for
publisher.py:
python publisher.py -h
Example output:
usage: publisher.py [-h]
project
{list,create,delete,publish,publish-with-custom-attributes,publish-with-futures,publish-with-error-handler,publish-with-batch-settings}
...
This application demonstrates how to perform basic operations on topics
with the Cloud Pub/Sub API.
For more information, see the README.md under /pubsub and the documentation
at https://cloud.google.com/pubsub/docs.
positional arguments:
project Your Google Cloud project ID
{list,create,delete,publish,publish-with-custom-attributes,publish-with-futures,publish-with-error-handler,publish-with-batch-settings}
list Lists all Pub/Sub topics in the given project.
create Create a new Pub/Sub topic.
delete Deletes an existing Pub/Sub topic.
publish Publishes multiple messages to a Pub/Sub topic.
publish-with-custom-attributes
Publishes multiple messages with custom attributes to
a Pub/Sub topic.
publish-with-futures
Publishes multiple messages to a Pub/Sub topic and
prints their message IDs.
publish-with-error-handler
Publishes multiple messages to a Pub/Sub topic with an
error handler.
publish-with-batch-settings
Publishes multiple messages to a Pub/Sub topic with
batch settings.
optional arguments:
-h, --help show this help message and exit
- Run the publisher script to create a Pub/Sub topic:
python publisher.py $GOOGLE_CLOUD_PROJECT create MyTopic
Example output:
Topic created: name: "projects/qwiklabs-gcp-fe27729bc161fb22/topics/MyTopic"
Click Check my progress to verify the objective.
Create a topic
- List all Pub/Sub topics in your project:
python publisher.py $GOOGLE_CLOUD_PROJECT list
Example output:
name: "projects/qwiklabs-gcp-fe27729bc161fb22/topics/MyTopic"
You can also view the topic you created in the Google Cloud console.
- In the Google Cloud console, on the Navigation menu, click Pub/Sub > Topics (or View all products > Analytics > Pub/Sub > Topics).
MyTopic appears in the list.
Task 5. Create a subscription
- Create a Pub/Sub subscription for
MyTopic using the subscriber.py script:
python subscriber.py $GOOGLE_CLOUD_PROJECT create MyTopic MySub
Click Check my progress to verify the objective.
Create a subscription
- List all subscriptions in your project:
python subscriber.py $GOOGLE_CLOUD_PROJECT list-in-project
Only one subscription appears because you created only one.
Example output:
projects/qwiklabs-gcp-7877af129f04d8b3/subscriptions/MySub
-
View the subscription you created in the Google Cloud console. In the Pub/Sub menu, click Subscriptions. The subscription name and other details appear.
-
Run the following command to view usage information for subscriber.py:
python subscriber.py -h
Example output:
usage: subscriber.py [-h]
project
{list_in_topic,list_in_project,create,create-push,delete,update,receive,receive-custom-attributes,receive-flow-control,receive-synchronously,listen_for_errors}
...
This application demonstrates how to perform basic operations on
subscriptions with the Cloud Pub/Sub API.
For more information, see the README.md under /pubsub and the documentation
at https://cloud.google.com/pubsub/docs.
positional arguments:
project Your Google Cloud project ID
{list_in_topic,list_in_project,create,create-push,delete,update,receive,receive-custom-attributes,receive-flow-control,receive-synchronously,listen_for_errors}
list_in_topic Lists all subscriptions for a given topic.
list_in_project Lists all subscriptions in the current project.
create Create a new pull subscription on the given topic.
create-push Create a new push subscription on the given topic.
delete Deletes an existing Pub/Sub topic.
update Updates an existing Pub/Sub subscription's push
endpoint URL. Note that certain properties of a
subscription, such as its topic, are not modifiable.
receive Receives messages from a pull subscription.
receive-custom-attributes
Receives messages from a pull subscription.
receive-flow-control
Receives messages from a pull subscription with flow
control.
receive-synchronously
Pulling messages synchronously.
listen_for_errors Receives messages and catches errors from a pull
subscription.
optional arguments:
-h, --help show this help message and exit
Task 6. Publish messages
Now that you've set up MyTopic (the topic) and a subscription to MyTopic (MySub), use gcloud commands to publish messages to MyTopic.
- Publish the message "Hello" to
MyTopic:
gcloud pubsub topics publish MyTopic --message "Hello"
- Publish additional messages to
MyTopic by running the following commands. Replace <YOUR NAME> with your name and <FOOD> with a food you like:
gcloud pubsub topics publish MyTopic --message "Publisher's name is <YOUR NAME>"
gcloud pubsub topics publish MyTopic --message "Publisher likes to eat <FOOD>"
gcloud pubsub topics publish MyTopic --message "Publisher thinks Pub/Sub is awesome"
Task 7. View messages
Now that you've published messages to MyTopic, pull and view the messages using MySub.
- Use
MySub to pull the messages from MyTopic:
python subscriber.py $GOOGLE_CLOUD_PROJECT receive MySub
Example output:
Listening for messages on projects/qwiklabs-gcp-7877af129f04d8b3/subscriptions/MySub
Received message: Message {
data: 'Publisher thinks Pub/Sub is awesome'
attributes: {}
}
Received message: Message {
data: 'Hello'
attributes: {}
}
Received message: Message {
data: "Publisher's name is Harry"
attributes: {}
}
Received message: Message {
data: 'Publisher likes to eat cheese'
attributes: {}
}
- Press CTRL+C to stop listening.
Task 8. Test your understanding
Answer the following questions to reinforce your understanding of the concepts in this lab.
Congratulations!
You used Python to create a Pub/Sub topic and subscription, published messages to the topic using gcloud, and used the subscription to pull messages from the topic.
Next steps / Learn more
This lab is part of a series of labs called Qwik Starts, which introduce you to many features available in Google Cloud. Search for "Qwik Starts" in the Google Skills catalog to find the next lab you'd like to take.
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated on November 30, 2026
Lab Last Tested October 30, 2026
Copyright 2026 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.