0
0
GCPcloud~5 mins

Topics and subscriptions in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to send messages between different parts of your app without them needing to be connected directly, you use topics and subscriptions. Topics are like message boards where you post messages, and subscriptions are like mailboxes that receive those messages.
When you want to send notifications from one service to many other services without waiting for them.
When you need to decouple parts of your app so they can work independently.
When you want to process messages asynchronously, like handling orders or user actions later.
When you want to broadcast updates to multiple systems at once.
When you want to ensure messages are delivered even if the receiver is temporarily offline.
Config File - topic-subscription.yaml
topic-subscription.yaml
apiVersion: pubsub.cnrm.cloud.google.com/v1beta1
kind: PubSubTopic
metadata:
  name: example-topic
spec:
  projectRef:
    name: example-project
---
apiVersion: pubsub.cnrm.cloud.google.com/v1beta1
kind: PubSubSubscription
metadata:
  name: example-subscription
spec:
  topicRef:
    name: example-topic
  ackDeadlineSeconds: 20
  retainAckedMessages: false
  messageRetentionDuration: 604800s

This file creates a Pub/Sub topic named example-topic where messages can be published.

It also creates a subscription named example-subscription that listens to that topic.

The subscription settings control how long to wait for acknowledgments and how long messages are kept.

Commands
This command creates a new topic named example-topic where messages can be sent.
Terminal
gcloud pubsub topics create example-topic
Expected OutputExpected
Created topic [projects/example-project/topics/example-topic].
This command creates a subscription named example-subscription that listens to messages from example-topic. The ack-deadline sets how long to wait for message acknowledgment.
Terminal
gcloud pubsub subscriptions create example-subscription --topic=example-topic --ack-deadline=20
Expected OutputExpected
Created subscription [projects/example-project/subscriptions/example-subscription].
--topic - Specifies the topic to subscribe to.
--ack-deadline - Sets the acknowledgment deadline in seconds.
This command sends a message 'Hello, world!' to the example-topic. Subscribers will receive this message.
Terminal
gcloud pubsub topics publish example-topic --message="Hello, world!"
Expected OutputExpected
messageIds: ["1234567890123456"]
--message - Specifies the message content to publish.
This command pulls one message from example-subscription and automatically acknowledges it, meaning the message is marked as received and won't be sent again.
Terminal
gcloud pubsub subscriptions pull example-subscription --auto-ack --limit=1
Expected OutputExpected
ACKNOWLEDGED 1 message(s). Received message: data: Hello, world! messageId: 1234567890123456
--auto-ack - Automatically acknowledges the pulled messages.
--limit - Limits the number of messages to pull.
Key Concept

If you remember nothing else from this pattern, remember: topics send messages and subscriptions receive them independently so your app parts don’t have to wait for each other.

Common Mistakes
Trying to pull messages from a subscription before creating it.
The subscription does not exist yet, so no messages can be received.
Always create the subscription first using the correct topic reference.
Publishing messages to a topic that does not exist.
Messages cannot be sent to a non-existent topic, causing errors.
Create the topic before publishing messages to it.
Not acknowledging messages after pulling them.
Messages remain unacknowledged and will be redelivered, causing duplicates.
Use --auto-ack flag or explicitly acknowledge messages after processing.
Summary
Create a topic to send messages to with 'gcloud pubsub topics create'.
Create a subscription to receive messages from the topic with 'gcloud pubsub subscriptions create'.
Publish messages to the topic with 'gcloud pubsub topics publish'.
Pull and acknowledge messages from the subscription with 'gcloud pubsub subscriptions pull'.