Process Flow - Publishing messages
Create Topic
Prepare Message
Publish Message to Topic
Message Stored in Topic
Subscribers Receive Message
This flow shows how a message is created, published to a topic, stored, and then delivered to subscribers.
from google.cloud import pubsub_v1 publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path('my-project', 'my-topic') future = publisher.publish(topic_path, b'My message data') print(future.result())
| Step | Action | Input | Result | State Change |
|---|---|---|---|---|
| 1 | Create Topic Path | project='my-project', topic='my-topic' | topic_path='projects/my-project/topics/my-topic' | Topic path ready for publishing |
| 2 | Prepare Message | Message data='My message data' | Message bytes ready | Message encoded as bytes |
| 3 | Publish Message | topic_path, message bytes | Future object returned | Message sent to Pub/Sub service |
| 4 | Wait for Result | Future object | Message ID received | Message stored in topic, ready for delivery |
| 5 | Message Delivery | Subscribers listening | Subscribers receive message | Message delivered to subscribers |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|---|
| publisher | None | PublisherClient instance | PublisherClient instance | PublisherClient instance | PublisherClient instance | PublisherClient instance |
| topic_path | None | 'projects/my-project/topics/my-topic' | 'projects/my-project/topics/my-topic' | 'projects/my-project/topics/my-topic' | 'projects/my-project/topics/my-topic' | 'projects/my-project/topics/my-topic' |
| message_data | None | None | b'My message data' | b'My message data' | b'My message data' | b'My message data' |
| future | None | None | None | Future object | Future object | Message ID string |
Publishing messages in GCP Pub/Sub: - Create a topic path with project and topic names - Prepare message data as bytes - Publish message asynchronously - Wait for confirmation via Future - Message stored and delivered to subscribers asynchronously