Skip to content

Commit

Permalink
Merge pull request dapr#4407 from hhunter-ms/issue_3321
Browse files Browse the repository at this point in the history
Add non-SDK Python code example for Bulk Subscribe
  • Loading branch information
hhunter-ms authored Nov 19, 2024
2 parents 1d7c51b + d6034fb commit 1d418a4
Showing 1 changed file with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,13 @@ Status | Description
`RETRY` | Message to be retried by Dapr
`DROP` | Warning is logged and message is dropped

Please refer [Expected HTTP Response for Bulk Subscribe]({{< ref pubsub_api.md >}}) for further insights on response.
Refer to [Expected HTTP Response for Bulk Subscribe]({{< ref pubsub_api.md >}}) for further insights on response.

### Example

Please refer following code samples for how to use Bulk Subscribe:

{{< tabs "Java" "JavaScript" ".NET" >}}
The following code examples demonstrate how to use Bulk Subscribe.

{{< tabs "Java" "JavaScript" ".NET" "Python" >}}
{{% codetab %}}

```java
Expand Down Expand Up @@ -471,7 +470,50 @@ public class BulkMessageController : ControllerBase

{{% /codetab %}}

{{% codetab %}}
Currently, you can only bulk subscribe in Python using an HTTP client.

```python
import json
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/dapr/subscribe', methods=['GET'])
def subscribe():
# Define the bulk subscribe configuration
subscriptions = [{
"pubsubname": "pubsub",
"topic": "TOPIC_A",
"route": "/checkout",
"bulkSubscribe": {
"enabled": True,
"maxMessagesCount": 3,
"maxAwaitDurationMs": 40
}
}]
print('Dapr pub/sub is subscribed to: ' + json.dumps(subscriptions))
return jsonify(subscriptions)
# Define the endpoint to handle incoming messages
@app.route('/checkout', methods=['POST'])
def checkout():
messages = request.json
print(messages)
for message in messages:
print(f"Received message: {message}")
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}
if __name__ == '__main__':
app.run(port=5000)
```

{{% /codetab %}}

{{< /tabs >}}

## How components handle publishing and subscribing to bulk messages

For event publish/subscribe, two kinds of network transfers are involved.
Expand Down

0 comments on commit 1d418a4

Please sign in to comment.