Skip to main content

Quickstart: Publish/Subscribe

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

pubsub qs

1

Prerequisites

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

2

Log in to Catalyst

Authenticate to Diagrid Catalyst using the following command:

diagrid login

Confirm your organization and user details are correct:

diagrid whoami
3

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.

4

Install Dependencies and Run

The diagrid dev run command creates your Catalyst project (if needed), provisions resources (App IDs, Components, Diagrid Pub/Sub service), configures environment variables, and launches your application using the dapr multi-app run file.

Install .NET dependencies:

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

Run the application:

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.

5

Call the Publish API

With the quickstart running, test the Publish API using curl or the REST Client extension with the test.rest file.

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 and disconnect the local app connection:

diagrid dev stop
6

Clean Up ResourcesOptional

If you are not going to continue to use this application, delete the app IDs:

diagrid appid delete publisher
diagrid appid delete subscriber

To delete the entire Catalyst project, including the managed infrastructure resources:

diagrid project delete dev-python-pubsub  # or dev-csharp-pubsub, dev-js-pubsub, dev-java-pubsub