Skip to main content

Quickstart: Publish/Subscribe

This quickstart showcases how to enable the publish/subscribe pattern using the Pub/Sub API.

pubsub qs

Prerequisites

Before you proceed, ensure you have the following prereqs installed.

Log in to Catalyst

Authenticate to Diagrid Catalyst using the following command:

diagrid login

Confirm your organization and user details are correct:

diagrid whoami

Clone quickstart code

Clone the quickstart code from the Diagrid Labs GitHub repository and navigate to the appropriate directory:

diagrid dev quickstart --type pubsub --language python

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 quickstart application

To run the quickstart locally, use the diagrid dev run command. This command uses the dapr multi-app run file in the root of the code directory to:

  • Provision a Catalyst project: If the project does not exist, the command will create a new project
  • Create resources: Creates the necessary resources on Catalyst such as App IDs, Components, and Diagrid Pub/Sub or KV Store managed services
  • Launch the quickstart applications: Locates the code directories using the values specified for workDir and launches each application using the app command inputs
  • Inject environment variables: Configures the Dapr client to talk to Catalyst using the publisher App ID API token
  • Establish Catalyst connectivity: Creates a private dev tunnel for the subscriber application on the specified appPort, which will route all traffic destined for the subscriber App ID to the local port where the code is running

For additional details on the Catalyst local development experience, read Develop Locally with Catalyst APIs.

Restore .NET package dependencies required by each service:

dotnet restore ./publisher && dotnet restore ./subscriber

Run your applications and connect the subscriber App ID to the local subscriber app using the following command.

diagrid dev run -f dev-python-pubsub.yaml --project dev-python-pubsub
tip

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.

logs

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.

topic explorer

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.