Complete the code to enable message ordering in a Pub/Sub topic.
topic = publisher.topic_path(project_id, [1]) publisher.create_topic(request={"name": topic, "message_ordering_enabled": true})
Setting the topic name to 'ordered-topic' and enabling message_ordering_enabled ensures messages are published in order.
Complete the code to publish a message with ordering key.
future = publisher.publish(topic, data=b"Hello, world!", ordering_key=[1])
The ordering key must be a string. Here, 'key1' is used to group messages for ordering.
Fix the error in subscriber code to enable message ordering.
subscriber = pubsub_v1.SubscriberClient() subscription_path = subscriber.subscription_path(project_id, subscription_name) flow_control = pubsub_v1.types.FlowControl(max_messages=10) streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback, flow_control=[1], enable_message_ordering=True)
The flow_control parameter must be passed as an object to control message flow and preserve ordering.
Fill both blanks to configure the subscriber to acknowledge messages in order.
def callback(message): print(f"Received message: {message.data.decode('utf-8')}") message.[1]() subscriber = pubsub_v1.SubscriberClient() subscription_path = subscriber.subscription_path(project_id, subscription_name) streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback, enable_message_ordering=[2])
Calling ack() acknowledges the message, and enabling enable_message_ordering=True ensures ordered delivery.
Fill all three blanks to create a topic, publish ordered messages, and enable ordering in subscriber.
topic = publisher.topic_path(project_id, [1]) publisher.create_topic(request={"name": topic, "message_ordering_enabled": [2]) future = publisher.publish(topic, data=b"Test", ordering_key=[3])
Use 'ordered-topic' as the topic name, enable message ordering with True, and publish with an ordering key 'key-123'.