Google Cloud Pub/Sub is a messaging service for exchanging event data among applications and services. A producer of data publishes messages to a Cloud Pub/Sub topic. A consumer creates a subscription to that topic. Subscribers either pull messages from a subscription or are configured as webhooks for push subscriptions. Every subscriber must acknowledge each message within a configurable window of time.
the GCP console opens in this tab.Note: You can view the menu with a list of GCP Products and Services by clicking the Navigation menu at the top-left, next to “Google Cloud Platform”.
The Google Cloud Shell
Activate Google Cloud Shell
Google 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. Google Cloud Shell provides command-line access to your GCP resources.
- In GCP console, on the top right toolbar, click the Open Cloud Shell button.
- In the dialog box that opens, click START CLOUD SHELL:You can click “START CLOUD SHELL” immediately when the dialog box opens.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud Platform. It comes pre-installed on Cloud Shell and supports tab-completion.
You can list the active account name with this command:
gcloud auth list
Credentialed accounts: - <myaccount>@<mydomain>.com (active)
Credentialed accounts: - firstname.lastname@example.org
You can list the project ID with this command:
gcloud config list project
[core] project = <project_ID>
[core] project = qwiklabs-gcp-44776a13dea667a6
Full documentation of gcloud is available on Google Cloud gcloud Overview.
Setting up Pub/Sub
You can use the Google Cloud Shell console to perform operations in Google Cloud Pub/Sub.
To use a Pub/Sub, you create a topic to hold data and a subscription to access data .published to the topic.
- Click Navigation menu > Pub/Sub > Topics.
- Click Create topic.
- The topic must have a unique name. For this lab, name your topic
MyTopicin the Create a topic dialog, then click CREATE TOPIC.
You’ve created a topic.
Add a subscription
Now you’ll make a subscription to access the topic.
- In the Topics dialog, for the topic you just made click the three dot icon > Create subscription.
- Type a name for the subscription, such as MySub, set the Delivery Type to Pull, then click Create.
Publish a message to the topic
- Click on the Topic you just made. In topic details page click on Publish Message at the top.
Hello Worldin the Message field and click Publish.
View the message
To view the message you’ll use the subscription (
MySub) to pull the message (
Hello World) from the topic (
Enter the following command in command line.
gcloud beta pubsub subscriptions pull --auto-ack MySub
The message appears in the DATA field of the command output.
You created a Pub/Sub topic, published to the topic, created a subscription, then used the subscription to pull data from the topic.