Quickstart: Publish/Subscribe
This quickstart showcases how to enable the publish/subscribe pattern using the Pub/Sub API.
Prerequisites
Before you proceed, ensure you have the following prereqs installed.
- Python
- .NET
- JavaScript
- Java
Log in to Catalyst
Authenticate to Diagrid Catalyst using the following command:
diagrid login
Confirm your organization and user details are correct:
diagrid whoami
Deploy quickstart project
Create a project and bootstrap the necessary resources to begin developing locally with the Pubsub API. This command:
✔️ Creates a new Catalyst project
✔️ Deploys a Diagrid Pub/Sub service
✔️ Creates App IDs: one for the publishing application and one for the subscribing application
✔️ Clones a quickstart application to your machine
✔️ Scaffolds a dev config file to run the quickstart code with the necessary Catalyst connectivity info
- Python
- .NET
- JavaScript
- Java
diagrid project quickstart --type pubsub --language python
diagrid project quickstart --type pubsub --language csharp
diagrid project quickstart --type pubsub --language javascript
diagrid project quickstart --type pubsub --language java
After the command executes successfully, the CLI will output the contents of your newly created project including the two App IDs publisher
and subscriber
and a Diagrid KV Store called pubsub
.
Run sample application
To run the quickstart code locally, you can use the diagrid dev start
command. This command:
- Launches the local quickstart apps: Locates the code directories using the values specified for
workDir
and launches each application using the respective app command inputs - Injects environment variables: Configures the Dapr client to talk to Catalyst using the
publisher
App ID API token - Establishes private dev tunnel: Creates a private tunnel for the
subscriber
application on the specifiedappPort
, which will route all traffic destined for thesubscriber
App ID to the local port where the code is running - Streams application logs: Displays connectivity logs along with real-time app feedback directly in the terminal to make API testing simple
For additional details on the Catalyst local development experience, read Develop Locally with Catalyst APIs.
- Python
- .NET
- JavaScript
- Java
Install python dependencies in a virtual environment.
This quickstart uses the venv module to create the virtual environment. Please feel free to adapt if you prefer to use conda, pipenv, or another alternative.
python -m venv venv
- MacOS
- Linux
- Windows
If you are using MacOS, start the virtual environment with the following command:
source venv/bin/activate
Don't forget to close your virtual environment with deactivate
when done with the quickstart
If you are using Linux, start the virtual environment with the following command:
source venv/bin/activate
Don't forget to close your virtual environment with deactivate
when done with the quickstart
If you are using PowerShell on Windows, start the virtual environment with the following command:
venv/Scripts/Activate.ps1
If you are using the Command Prompt on Windows, start the virtual environment with the following command:
venv/Scripts/activate.bat
If running in the Windows Command Prompt, don't forget to close your virtual environment with venv/Scripts/deactivate.bat
when done with the quickstart
Install the python packages required by each service:
pip install -r ./publisher/requirements.txt && pip install -r ./subscriber/requirements.txt
Restore .NET package dependencies required by each service:
donet restore ./publisher && donet restore ./subscriber
Install node packages and dependencies required by each service:
npm install --prefix ./publisher && npm install --prefix ./subscriber
Download and install maven dependencies required by each service:
mvn clean install -f ./publisher && mvn clean install -f ./subscriber
Run your applications and connect the subscriber
App ID to the local subscriber app using the following command.
diagrid dev start
Ensure the log Connected App ID "subscriber" to localhost:5002
appears before proceeding to the next section.
Call the Publish API
With the quickstart application running, use the curl commands below or take advantage of the test.rest
file in the root folder of the repo powered by the VS Code REST Client extension to trigger requests from the app to the Dapr Publish API hosted by Catalyst.
Open a new terminal and execute the following curl command:
curl -i -X POST http://localhost:5001/order -H "Content-Type: application/json" -d '{"orderId":1}'
Upon successful execution, the publisher app logs should show an indication that a message was successfully published, and the subscriber app logs should indicate the message was successfully received.
You can also view the published messages using the Diagrid Pub/Sub topic explorer. When the Pending Message Count
is 0, it means the subscriber app has successfully processed all messages on the orders
topic.
To stop the applications use CTL-C
and to disconnect the local app connection from the subscriber
App ID run the following:
diagrid dev stop
Clean up resources
If you are not going to continue to use this application, you can delete the Catalyst resoures using the following commands:
diagrid appid delete publisher
diagrid appid delete subscriber
If you want to delete the entire Catalyst project, including the managed infrastructure resources, run the diagrid project delete
command.