0
0
GcpHow-ToBeginner · 4 min read

How to Subscribe to Google Cloud Pub/Sub: Step-by-Step Guide

To subscribe to a Google Cloud Pub/Sub topic, create a subscription linked to the topic using the gcloud pubsub subscriptions create command or client libraries. Then, pull messages from the subscription or set up a push endpoint to receive messages automatically.
📐

Syntax

To create a subscription, use the following command syntax:

  • gcloud pubsub subscriptions create SUBSCRIPTION_NAME --topic=TOPIC_NAME: Creates a subscription named SUBSCRIPTION_NAME for the topic TOPIC_NAME.
  • gcloud pubsub subscriptions pull SUBSCRIPTION_NAME --limit=NUM: Pulls NUM messages from the subscription.

In code, use client libraries to create subscriptions and receive messages.

bash
gcloud pubsub subscriptions create my-subscription --topic=my-topic

gcloud pubsub subscriptions pull my-subscription --limit=5
💻

Example

This example shows how to create a subscription and pull messages using Python client library.

python
from google.cloud import pubsub_v1

project_id = "your-project-id"
topic_id = "my-topic"
subscription_id = "my-subscription"

subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)

topic_path = subscriber.topic_path(project_id, topic_id)

# Create subscription
try:
    subscriber.create_subscription(name=subscription_path, topic=topic_path)
    print(f"Subscription created: {subscription_path}")
except Exception as e:
    print(f"Subscription may already exist: {e}")

# Pull messages
response = subscriber.pull(subscription=subscription_path, max_messages=5)

for received_message in response.received_messages:
    print(f"Received message: {received_message.message.data.decode('utf-8')}")
    subscriber.acknowledge(subscription=subscription_path, ack_ids=[received_message.ack_id])
Output
Subscription created: projects/your-project-id/subscriptions/my-subscription Received message: Hello, Pub/Sub! Received message: Another message
⚠️

Common Pitfalls

  • Not creating a subscription before pulling messages causes errors.
  • Pulling messages without acknowledging them leads to repeated delivery.
  • Using the wrong project or topic name causes failures.
  • For push subscriptions, forgetting to set up a valid HTTPS endpoint will prevent message delivery.
bash
## Wrong: Pulling without subscription

gcloud pubsub subscriptions pull missing-subscription --limit=1

## Right: Create subscription first

gcloud pubsub subscriptions create my-subscription --topic=my-topic

gcloud pubsub subscriptions pull my-subscription --limit=1
📊

Quick Reference

CommandDescription
gcloud pubsub subscriptions create SUBSCRIPTION_NAME --topic=TOPIC_NAMECreate a subscription for a topic
gcloud pubsub subscriptions pull SUBSCRIPTION_NAME --limit=NUMPull messages from a subscription
subscriber.create_subscription(name, topic)Create subscription in client library
subscriber.pull(subscription, max_messages)Pull messages programmatically
subscriber.acknowledge(subscription, ack_ids)Acknowledge received messages

Key Takeaways

Always create a subscription before trying to receive messages.
Use pull or push methods to receive messages from Pub/Sub subscriptions.
Acknowledge messages after processing to avoid redelivery.
Verify project and topic names to prevent errors.
For push subscriptions, ensure your endpoint is reachable and secure.