python-sdk/examples/pubsub-streaming at main · dapr/python-sdk

Example - Publish and subscribe to messages

This example utilizes a publisher and a subscriber to show the bidirectional pubsub pattern. It creates a publisher and calls the publish_event method in the DaprClient. In the ssubscriber.py file it creates a subscriber object that can call the next_message method to get new messages from the stream. After processing the new message, it returns a status to the stream.

Note: Make sure to use the latest proto bindings

Pre-requisites

Install Dapr python-SDK

Run example where users control reading messages off the stream

Run the following command in a terminal/command prompt:

# 1. Start Subscriber
dapr run --app-id python-subscriber --app-protocol grpc -- python3 subscriber.py  --topic=TOPIC_A1

In another terminal/command prompt run:

# 2. Start Publisher
dapr run --app-id python-publisher --app-protocol grpc --dapr-grpc-port=3500 --enable-app-health-check -- python3 publisher.py --topic=TOPIC_A1

Run example with a handler function

Run the following command in a terminal/command prompt:

# 1. Start Subscriber
dapr run --app-id python-subscriber --app-protocol grpc -- python3 subscriber-handler.py  --topic=TOPIC_A2

In another terminal/command prompt run:

# 2. Start Publisher
dapr run --app-id python-publisher --app-protocol grpc --dapr-grpc-port=3500 --enable-app-health-check -- python3 publisher.py  --topic=TOPIC_A2

Cleanup