Create and test a Pub\/Sub queue with a subscriber

Introduction

This project focuses on creating and testing a Pub/Sub queue with a subscriber in Google Cloud. Pub/Sub (Publish/Subscribe) is a messaging service that allows you to send and receive messages between independent applications. It’s useful because:

  • Decoupling: Publishers and subscribers do not need to interact directly.
  • Scalability: It can handle large volumes of messages and scale seamlessly.
  • Reliability: Provides at-least-once message delivery to ensure messages are not lost.

Prerequisites

To complete this project on the Google Cloud free tier (no credits required), you will need:

Google Cloud Account Sign up at https://cloud.google.com/ if you do not already have an account.
Ensure you are using the free tier (Google often provides a small monthly usage allowance for Pub/Sub).

Google Cloud SDK (Command-Line Interface)Install the SDK on your local machine: https://cloud.google.com/sdk/docs/install.

Project PermissionsYou need Owner or Editor roles on your Google Cloud project.
Alternatively, you can have Pub/Sub Admin role for the specific tasks.

APIs EnabledMake sure the Cloud Pub/Sub API is enabled in your project.
Also ensure that the Container Registry API is enabled if you plan to containerize or automate further (not strictly required here, but often used in broader setups).

Step 1. Set Up Your Environment

GUI (Cloud Console)

Go to the Google Cloud Console: https://console.cloud.google.com/.

Make sure you select the correct project (top-left corner) where you will create the Pub/Sub resources.

In the left navigation menu, click on APIs & Services > Library.

Search for “Cloud Pub/Sub API” and click on Enable if it’s not already enabled.


CLI (gcloud)

Open a terminal where the Google Cloud SDK is installed and run:

# Set your project ID (replace YOUR_PROJECT_ID with your actual project ID):
gcloud config set project YOUR_PROJECT_ID

# Enable the Pub/Sub API:
gcloud services enable pubsub.googleapis.com

Step 2. Create a Pub/Sub Topic

GUI (Cloud Console) :

In the Cloud Console, go to the left navigation menu and select Pub/Sub > Topics.

Click “Create topic”.

Enter a Topic ID, for example: my-pubsub-topic.

You can leave the Region as “Global” or choose a region.

Click “Create” to finish.


CLI (gcloud) :

# Create a Pub/Sub topic named my-pubsub-topic
gcloud pubsub topics create my-pubsub-topic

Step 3. Create a Subscription

GUI (Cloud Console) :

In the Pub/Sub section, click on Subscriptions.

Click “Create subscription”.

Under Subscription ID, enter a name, for example: my-subscription.

In the Topic drop-down, select my-pubsub-topic (the topic you just created).

For Delivery type, choose Pull or Push. For simplicity, select Pull for this guide.

Leave other settings as default, or configure as needed.

Click “Create” to finalize the subscription.


CLI (gcloud) :

# Create a pull subscription named my-subscription for the topic my-pubsub-topic
gcloud pubsub subscriptions create my-subscription \
--topic=my-pubsub-topic \
--ack-deadline=10

Step 4. Publish a Test Message

GUI (Cloud Console) :

Return to Pub/Sub > Topics.

Click on the my-pubsub-topic.

Click “Publish message”.

In the Message body field, type something like:
Hello from Pub/Sub

Click “Publish”.


CLI (gcloud) :

# Publish a message to the my-pubsub-topic
gcloud pubsub topics publish my-pubsub-topic --message "Hello from Pub/Sub"

Step 5. Pull the Message from the Subscription

GUI (Cloud Console) :

Go to Pub/Sub > Subscriptions.

Click on my-subscription.

Look for an option to Pull messages or View messages.

You should see the message “Hello from Pub/Sub” in the list if not acknowledged.

Note: The console might not always show the “Pull” feature directly. If you do not see the message, proceed with the CLI to confirm reception.



CLI (gcloud) :

# Pull messages from the my-subscription subscription:
gcloud pubsub subscriptions pull my-subscription --auto-ack

Step 6. Verifying and Testing the Project

To verify:

  • Check that messages are correctly published to the topic without errors.
  • Ensure the subscriber receives (or can pull) the messages: You should see the “Hello from Pub/Sub” message.
  • Try multiple messages to confirm consistent behavior.

Additional testing tips:

  • If using a Push subscription, set up a small web service endpoint and verify it receives the payload.
  • If using Pull subscriptions, confirm you can retrieve messages repeatedly.

Conclusion

In this guide, we :

Enabled the Pub/Sub API and set up your Google Cloud environment.

Created a Pub/Sub topic and subscription (Pull-based).

Published messages and verified they were received by the subscriber.

Learned how to troubleshoot common issues.

What is Cloud Computing ?

Cloud computing delivers computing resources (servers, storage, databases, networking, and software) over the internet, allowing businesses to scale and pay only for what they use, eliminating the need for physical infrastructure.


  • AWS: The most popular cloud platform, offering scalable compute, storage, AI/ML, and networking services.
  • Azure: A strong enterprise cloud with hybrid capabilities and deep Microsoft product integration.
  • Google Cloud (GCP): Known for data analytics, machine learning, and open-source support.