Tutorial: Publish/Subscribe API
This tutorial showcases how to enable the publisher-subscriber pattern between two locally-running applications via the Diagrid Pub/Sub API.
To complete this tutorial you will:
- Create Diagrid resources required to enable Pub/Sub messaging between two applications
- Establish a connection between the application services and associated Catalyst App IDs
- Test the Publish/Subscribe API to ensure messages are successfully published and delivered
Prerequisites
Before you proceed with the tutorial, ensure you have the appropriate prerequisites installed for the language you would like to target.
- .NET
- Python
- JavaScript
- Java
- Install the Diagrid CLI
- Download Git
- Install .NET 6+ SDK
- Install the Diagrid CLI
- Download Git
- Install Python 3.11+
- Install the Diagrid CLI
- Download Git
- Install latest Node.js
- Install the Diagrid CLI
- Download Git
- Install Java 11+: Oracle JDK or Open JDK
- Install Apache Maven 3.9.5+
Clone the samples repo
Begin by cloning the samples repository to your local machine with the following command:
git clone https://github.com/diagridio/catalyst-samples.git
Login to Diagrid Catalyst
Authenticate to Diagrid Catalyst using the following command:
diagrid login
Confirm your organization and user details are correct:
diagrid whoami
Create Catalyst resources
In this section, you will create the following resources in your Diagrid account:
- A Diagrid project, which acts as a logical container into which Catalyst resources like App IDs, Connections, etc. are deployed.
- A Diagrid Pub/Sub Broker,
pubsub
, which will be used to enable asynchronous messaging between applications. - An App ID,
publisher
, which will be used by the local publisher app to publish messages to the underlying broker. - A second App ID,
consumer
and an associatedpub/sub subscription
which will route published messages to your local consumer app.
Create project
If you do not have an existing project available within your organization, create a new Catalyst project and deploy the default pub/sub broker.
diagrid project create catalyst-project --deploy-managed-pubsub
If you have a project already available, ensure the managed broker is deployed. If not, you can create it using the following command:
diagrid pubsub create pubsub
To set this project as the default project in the Diagrid CLI, run:
diagrid project use catalyst-project
Create Application Identities
In Diagrid Catalyst, each application is represented by a remote identity, known as an App ID
. An App ID functions as the single point of contact for all interactions between an application and the Catalyst APIs. Before diving into the application code, create two App IDs in Diagrid Catalyst, one to represent the publishing app, and one to represent the subscribing app.
diagrid appid create publisher
diagrid appid create consumer
Create Pub/Sub Topic Subscription
With the Diagrid Pub/Sub Broker already provisioned in your project, the next step is to create a topic subscription through which the consumer
App ID can subscribe to messages sent by the publisher
.
Use the following command to ensure all messages sent to the orders
topic in the message broker are routed to the /pubsub/neworders
endpoint of the consumer application:
diagrid subscription create pubsub-consumer --connection pubsub --topic orders --route /pubsub/neworders --scopes consumer
Connect your applications to Catalyst
In this section, we will connect our local applications to Catalyst to enable messaging through the Pub/Sub API using Diagrid-managed resources.
Create dev config file
To easily run your applications and connect them to their respective App IDs using a single command, you can use a Catalyst dev config file. This is a YAML file that will be automatically generated based on the App IDs created in the previous section.
When used with the diagrid dev start
command, the dev config file will:
- Run the
publisher
andconsumer
applications with all required environment variables needed to connect the Dapr SDK to Diagrid Catalyst. - Create a Local App Connection for the
consumer
application on the localappPort
, which will route all traffic destined for theconsumer
App ID to the local port where the code is running.
For additional details on using the dev config file, read Develop Locally with Catalyst APIs.
- .NET
- Python
- JavaScript
- Java
Navigate to the root directory of the csharp app and install all dotnet dependencies.
cd csharp
dotnet build
Set up your local Catalyst development environment by running the following scaffold command:
diagrid dev scaffold
Open the auto-generated YAML file and add or update the publisher
App ID section with the following:
workDir
: current directoryenv.ASPNETCORE_URLS
: the port your application will listen oncommand
: the application run command
In addition, remove the env.appPort
. The publisher is making an inbound call to the Catalyst APIs and does not require an app connection to your the local machine.
Ensure you do not overwrite any of the other values in the file during this process.
- appId: publisher
env:
DAPR_API_TOKEN: <publisher-api-token>
DAPR_APP_ID: publisher
DAPR_CLIENT_TIMEOUT_SECONDS: 10
DAPR_GRPC_ENDPOINT: <grpc-endpoint>
DAPR_HTTP_ENDPOINT: <http-endpoint>
ASPNETCORE_URLS: http://localhost:5001
workDir: .
command: ["dotnet", "run"]
Similarily, add or update the consumer
App ID section with the following:
workDir
: current directoryenv.ASPNETCORE_URLS
: the port your application will listen oncommand
: the application run commandappPort
: used to establish an app connection from Catalyst to the locally-running consumer app
- appId: consumer
appPort: 5002
env:
DAPR_API_TOKEN: <consumer-api-token>
DAPR_APP_ID: consumer
DAPR_CLIENT_TIMEOUT_SECONDS: 10
DAPR_GRPC_ENDPOINT: <grpc-endpoint>
DAPR_HTTP_ENDPOINT: <http-endpoint>
ASPNETCORE_URLS: http://localhost:5002
workDir: .
command: ["dotnet", "run"]
Run your applications and connect the consumer
App ID to the local consumer app using the following command.
diagrid dev start
Ensure the log Connected App ID "consumer" to localhost:5002
appears before proceeding to the next section.
Navigate to the root directory of the python app and install all python dependencies in a virtual environment.
Note: This tutorial is adapted to use the venv module to create the virtual environment. Please feel free to adapt if you prefer to use conda, pipenv, etc alternatively.
cd python
python -m venv venv
- MacOS
- Linux
- Windows
source venv/bin/activate
Don't forget to close your virtual environment with deactivate
when done with the tutorial
source venv/bin/activate
Don't forget to close your virtual environment with deactivate
when done with the tutorial
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 tutorial
Install the python app requirements:
pip install -r requirements.txt
Set up your local Catalyst development environment by running the following scaffold command:
diagrid dev scaffold
Open the auto-generated YAML file and update the publisher
App ID section with the following:
workDir
: current directorycommand
: the application run command
In addition, remove the env.appPort
. The publisher is making an inbound call to the Catalyst APIs and does not require an app connection to your the local machine.
Ensure you do not overwrite any of the other values in the file during this process.
- appId: publisher
env:
DAPR_API_TOKEN: <publisher-api-token>
DAPR_APP_ID: publisher
DAPR_CLIENT_TIMEOUT_SECONDS: 10
DAPR_GRPC_ENDPOINT: <grpc-endpoint>
DAPR_HTTP_ENDPOINT: <http-endpoint>
workDir: .
command: ["uvicorn", "main:app", "--port", "5001"]
Similarily, update the consumer
App ID section with the following:
workDir
: current directorycommand
: the application run commandappPort
: used to establish an app connection from Catalyst to the locally-running consumer app
- appId: consumer
appPort: 5002
env:
DAPR_API_TOKEN: <consumer-api-token>
DAPR_APP_ID: consumer
DAPR_CLIENT_TIMEOUT_SECONDS: 10
DAPR_GRPC_ENDPOINT: <grpc-endpoint>
DAPR_HTTP_ENDPOINT: <http-endpoint>
workDir: .
command: ["uvicorn", "main:app", "--port", "5002"]
Run your applications and connect the consumer
App ID to the local consumer app using the following command.
diagrid dev start
Ensure the log Connected App ID "consumer" to localhost:5002
appears before proceeding to the next section.
Navigate to the root directory of the javascript app and clean install all node dependencies.
cd javascript
npm ci
Set up your local Catalyst development environment by running the following scaffold command:
diagrid dev scaffold
Open the auto-generated YAML file and add or update the publisher
App ID section with the following:
workDir
: current directoryenv.PORT
: the port your application will listen oncommand
: the application run command
In addition, remove the env.appPort
. The publisher is making an inbound call to the Catalyst APIs and does not require an app connection to your the local machine.
Ensure you do not overwrite any of the other values in the file during this process.
- appId: publisher
env:
DAPR_API_TOKEN: <publisher-api-token>
DAPR_APP_ID: publisher
DAPR_CLIENT_TIMEOUT_SECONDS: 10
DAPR_GRPC_ENDPOINT: <grpc-endpoint>
DAPR_HTTP_ENDPOINT: <http-endpoint>
PORT: 5001
workDir: .
command: ["npm", "run", "start"]
Similarily, add or update the consumer
App ID section with the following:
workDir
: current directoryenv.PORT
: the port your application will listen oncommand
: the application run commandappPort
: used to establish an app connection from Catalyst to the locally-running consumer app
- appId: consumer
appPort: 5002
env:
DAPR_API_TOKEN: <consumer-api-token>
DAPR_APP_ID: consumer
DAPR_CLIENT_TIMEOUT_SECONDS: 10
DAPR_GRPC_ENDPOINT: <grpc-endpoint>
DAPR_HTTP_ENDPOINT: <http-endpoint>
PORT: 5002
workDir: .
command: ["npm", "run", "start"]
Run your applications and connect the consumer
App ID to the local consumer app using the following command.
diagrid dev start
Ensure the log Connected App ID "consumer" to localhost:5002
appears before proceeding to the next section.
Navigate to the root directory of the java app and install all maven dependencies.
cd java
mvn clean install
Set up your local Catalyst development environment by running the following scaffold command:
diagrid dev scaffold
Open the auto-generated YAML file and update the publisher
App ID section with the following:
workDir
: current directorycommand
: the application run command
In addition, remove the env.appPort
. The publisher is making an inbound call to the Catalyst APIs and does not require an app connection to your the local machine.
Ensure you do not overwrite any of the other values in the file during this process.
- appId: publisher
env:
DAPR_API_TOKEN: <publisher-api-token>
DAPR_APP_ID: publisher
DAPR_CLIENT_TIMEOUT_SECONDS: 10
DAPR_GRPC_ENDPOINT: <grpc-endpoint>
DAPR_HTTP_ENDPOINT: <http-endpoint>
workDir: .
command: ["java", "-jar", "target/Main-0.0.1-SNAPSHOT.jar", "--port=5001"]
Similarily, update the consumer
App ID section with the following:
workDir
: current directorycommand
: the application run commandappPort
: used to establish an app connection from Catalyst to the locally-running consumer app
- appId: consumer
appPort: 5002
env:
DAPR_API_TOKEN: <consumer-api-token>
DAPR_APP_ID: consumer
DAPR_CLIENT_TIMEOUT_SECONDS: 10
DAPR_GRPC_ENDPOINT: <grpc-endpoint>
DAPR_HTTP_ENDPOINT: <http-endpoint>
workDir: .
command: ["java", "-jar", "target/Main-0.0.1-SNAPSHOT.jar", "--port=5002"]
Run your applications and connect the consumer
App ID to the local consumer app using the following command.
diagrid dev start
Ensure the log Connected App ID "consumer" to localhost:5002
appears before proceeding to the next section.
Interact with Catalyst APIs
With the applications now successfully running, it's time to test the publisher and consumer App IDs. To kick off the test, post a simple payload to the publisher app which then gets published as a message for the consumer to receive. Use the curl command below or take advantage of the REST Client extension using Visual Studio Code with the test.rest
file in the root folder of the repo.
Open a new terminal and execute the following curl command:
curl -i -X POST http://localhost:5001/pubsub/orders -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 consumer app logs should indicate the message was successfully received.
- .NET
- Python
- JavaScript
- Java
To stop the applications use CTL-C
and to disconnect the local app connection from the consumer
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 consumer
If you want to delete the entire Catalyst project, including the managed infrastructure resources, run the diagrid project delete
command.
diagrid project delete catalyst-project