Using Pubsub to publish messages

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”. 

Cloud Console Menu

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.

  1. In GCP console, on the top right toolbar, click the Open Cloud Shell button.
  2. 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:

Cloud Shell Terminal

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

Output:

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)

Example output:

Credentialed accounts:
 - google1623327_student@qwiklabs.net

You can list the project ID with this command:

gcloud config list project

Output:

[core]
project = <project_ID>

Example output:

[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.

  1. Click Navigation menu > Pub/Sub > Topics.
naviga_pubsub.png
  1. Click Create topic.
create_topic.png
  1. The topic must have a unique name. For this lab, name your topic MyTopic in the Create a topic dialog, then click CREATE TOPIC.
topic.png

You’ve created a topic.

topic_created.png

Add a subscription

Now you’ll make a subscription to access the topic.

  1. In the Topics dialog, for the topic you just made click the three dot icon > Create subscription.
create_subscription.png
  1. Type a name for the subscription, such as MySub, set the Delivery Type to Pull, then click Create.
subscription.png

Publish a message to the topic

  1. Click on the Topic you just made. In topic details page click on Publish Message at the top.
publish_message.png
  1. Enter Hello World in the Message field and click Publish.
publish.png

View the message

To view the message you’ll use the subscription (MySub) to pull the message (Hello World) from the topic (MyTopic).

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.

6f1565505c570a0d.png

You created a Pub/Sub topic, published to the topic, created a subscription, then used the subscription to pull data from the topic.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s