|
|
||
|---|---|---|
| .. | ||
| components | ||
| messages | ||
| .gitignore | ||
| Dockerfile | ||
| README.md | ||
| app.js | ||
| config.yaml | ||
| package.json | ||
README.md
Dapr Pub-Sub routing
In this quickstart, you'll run a subscriber application that makes use of Pub-Sub routing. The application can be quickly changed to:
- Toggle between programmatic and declarative subscriptions (or use both as long as there is no overlap)
- Add other routing rules with different CEL expressions
- Enable/disable routing (disable
PubSub.Routinginconfig.yaml)
Note: Because this example is intended to be something to play around with, it makes use of the
in-memorypubsub component and is not intended to be deployed to Kubernetes.
Install dependencies
npm install
Run the application
dapr run --app-id pubsub-routing --config config.yaml --components-path ./components --app-port 3000 node app.js
Switch between programmatic and declarative subscriptions
In app.js, the handler for /dapr/subscribe returns the programmatic subscriptions. The contents are commented out to demonstrate usage of a declarative subscription in components/subscription.yaml.
You can switch from declarative to programmatic subscriptions by:
- Changing
kind: Subscriptiontokind: Subscription_disabledincomponents/subscription.yamlso thatdaprddoes not load the file - Uncommenting the JSON response for
/dapr/subscribeinapp.js - Stop (CTRL-C) and restart the application using the
dapr runcommand above
Publish messages to route
Try the following curl commands in a separate terminal.
Publish a widget
curl -s http://localhost:3000/publish -H Content-Type:application/json --data @messages/widget.json
Publish a gadget
curl -s http://localhost:3000/publish -H Content-Type:application/json --data @messages/gadget.json
Publish a thingamajig
curl -s http://localhost:3000/publish -H Content-Type:application/json --data @messages/thingamajig.json
dapr stop --app-id pubsub-routing