Google Cloud Messaging Essentials

Introduction

In this lesson, we will explore the core concepts and operations of Google Cloud Pub/Sub, a messaging service that enables reliable, scalable, and asynchronous communication between different parts of your applications. Cloud Pub/Sub is designed around the concepts of topics and subscriptions, allowing you to publish messages to a topic and have them delivered to one or more subscribers. We will cover how to publish messages, configure subscriptions, and manage message delivery and acknowledgment. By the end of this lesson, you will understand how to use this messaging service effectively to build robust and decoupled systems.

Publishing Messages to Topics

To send information through Google Cloud Pub/Sub, you publish messages to a topic.

Publishing a message to a Cloud Pub/Sub topic:

from google.cloud import pubsub_v1

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path('your-project-id', 'your-topic-name')

future = publisher.publish(topic_path, b'Hello world!')
print(f'Message published: {future.result()}')

Output:

Message published: 123456789012345678

You can also include custom attributes with your message:

future = publisher.publish(
    topic_path,
    b'Hello with attributes!',
    author='John Doe',
    weeks_on_job='10'
)
print(f'Message published: {future.result()}')

Output:

Message published: 123456789012345679

Sending Multiple Messages at a Time

When you need to send multiple messages efficiently, Cloud Pub/Sub supports batch publishing. This allows you to publish several messages in a single request, reducing network overhead and improving throughput.

Batch publishing messages to a Cloud Pub/Sub topic:

from google.cloud import pubsub_v1

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path('your-project-id', 'your-topic-name')

messages = [
    (b'This is the content of message 1', {'author': 'Jane Doe'}),
    (b'This is the content of message 2', {'author': 'John Smith'}),
    # Add more messages as needed
]

futures = []
for data, attrs in messages:
    future = publisher.publish(topic_path, data, **attrs)
    futures.append(future)

for future in futures:
    print(f'Message published: {future.result()}')

Output:

Message published: 123456789012345680
Message published: 123456789012345681

Batching is handled automatically by the client library, but you can also configure batching settings for more control if needed.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal