Step 4: Try a Publish-Subscribe / Fan-Out Message Exchange Pattern
You've now completed Step 3: Set up the Solace Try-Me CLI Tool and learned a bit about Solly's Coffee.
In step 4, you'll use the Solace Try-Me CLI tool to take a closer look at the Solly's Coffee order management system (OMS) and set up a publish-subscribe message exchange to fan out event messages to many subscribers.
With publish-subscribe messaging, messages sent by the producer (publisher) can be processed multiple times by different consumers (subscribers). Each consumer receives its own copy of the message for processing, as illustrated in the following diagram:
In the Solly's Coffee OMS we introduced in step 3, the new order propagation event flow involving the OM-Order Details event uses publish-subscribe message exchange. The order details event is published by the Order Management application and received by both the Kitchen Floor and Barista Station applications.
Set Up a Publish-Subscribe Message Exchange
In this exercise we'll set up four applications, three subscribers and one publisher, and publish an event. The subscriber applications are Barista Station, Kitchen Floor, and Human Resources. The publisher is Order Management. After you complete these steps you'll have a message exchange pattern like the following animation:
To set up your applications and publish an event, perform these steps:
- Open four command-line terminal windows, one for each application.
- In terminal 1, subscribe the Barista Station application to all events from the country ca from all source types. Enter the following command:
- In terminal 2, subscribe the Kitchen Floor application to all events from the country ca with from all source types. Enter the following command:
- In terminal 3, subscribe the Human Resources application to all staff arrival events in the country ca. Enter the following command:
- Before configuring the publisher, create a message payload to publish in JSON format. Copy and paste the following text into a text editor and save the file as
order.json
.{ "source": "mobile", "device": "app-android", "country" : "Canada", "region" : "Ontario", "storeID" : 437, "requestId": 44334, "customer": { "name": "John Doe", "email": "john.doe@example.com", "phone": "123-456-7890" }, "items": [ { "productID": "COF001", "productName": "Americano", "quantity": 2, "size": "Medium", "options": ["Extra Shot", "Whipped Cream"] }, { "productID": "PST001", "productName": "Chocolate Croissant", "quantity": 1 } ], "totalPrice": 12.50, "pickupTime": "2024-01-05T14:30:00", "specialInstructions": "Please make it extra hot." }
- In terminal 4, publish an event with the message payload you just created from the Order Management application with the topic
coffeeshop/order/details/v1/{mobile}/{ca}/{437}/{44334}
. Enter the following command:stm send --topic coffeeshop/order/details/v1/mobile/ca/437/44334 --file order.json --client-name "Order Management"
You should see the following result:
ℹ info info: loading 'send' command from configuration 'stm-cli-config.json' … waiting connecting to broker [ws://localhost:8008, vpn: default, username: default, password: ******] ℹ info info: client name: Order Management ✔ success success: === Order Management successfully connected and ready to publish events. === … waiting publishing... ✔ success success: message published to topic - [Topic coffeeshop/order/details/v1/mobile/ca/437/44334], type - TEXT ℹ info info: Message Properties Destination: [Topic coffeeshop/order/details/v1/mobile/ca/437/44334] ℹ info info: Message Payload (bytes): 659 ✔ success success: disconnecting from Solace PubSub+ Event Broker... ✔ success success: disconnected ✔ success success: exiting...
stm receive --topic "coffeeshop/order/details/v1/*/ca/>" --client-name "Barista Station" --output-mode full
You should see the following result:
ℹ info info: loading 'receive' command from configuration 'stm-cli-config.json' … awaiting connecting to broker [ws://localhost:8008, vpn: default, username: default, password: ******] ℹ info info: client name: Barista Station ✔ success success: === Barista Station successfully connected and ready to receive events. === ℹ info info: subscribing to coffeeshop/order/details/v1/*/ca/> ℹ info info: press Ctrl-C to exit ✔ success success: successfully subscribed to topic: coffeeshop/order/details/v1/*/ca/>
stm receive --topic "coffeeshop/order/details/v1/*/ca/>" --client-name "Kitchen Floor"
You should see the following result:
ℹ info: loading 'receive' command from configuration 'stm-cli-config.json' … connecting to broker [ws://localhost:8008, vpn: default, username: default, password: ******] ℹ info: client name: Kitchen Floor ✔ success: === Kitchen Floor successfully connected and ready to receive events. === ℹ info: subscribing to coffeeshop/order/details/v1/*/ca/> ℹ info: press Ctrl-C to exit ✔ success: successfully subscribed to topic: coffeeshop/order/details/v1/*/ca/>
stm receive --topic "coffeeshop/staff/arrive/v2/*/ca/>" --client-name "Human Resources"
You should see the following result:
ℹ info info: loading 'receive' command from configuration 'stm-cli-config.json' … awaiting connecting to broker [ws://localhost:8008, vpn: default, username: default, password: ******] ℹ info info: client name: Human Resources ✔ success success: === Human Resources successfully connected and ready to receive events. === ℹ info info: subscribing to coffeeshop/staff/arrive/v2/*/ca/> ℹ info info: press Ctrl-C to exit ✔ success success: successfully subscribed to topic: coffeeshop/staff/arrive/v2/*/ca/>
Now, let's check the subscribers to see if they received the event published by the Order Management application.
To check your subscribers, perform these steps:
- Check the Barista Station application in terminal 1. The following message should appear:
- Check the Kitchen Floor application in terminal 2. The following message should appear:
- Check the Human Resources application in terminal 3. No message should appear because the subscription does not match the topic of the published message.
-
Press Control+C (on Windows and Linux) or Command+C (on Mac), to stop the applications.
✔ success: message Received - [Topic coffeeshop/order/details/v1/mobile/ca/437/44334], type - TEXT ℹ info: Message Properties Destination: [Topic coffeeshop/order/details/v1/mobile/ca/437/44334] Class Of Service: COS1 DeliveryMode: DIRECT Message Id: 1 DMQ Eligible Binary Attachment: len=664 ℹ info: Message Payload: { source: "mobile", device: "app-android", country: "Canada", region: "Ontario", storeID: 437, requestId: 44334, customer: { name: "John Doe", email: "john.doe@example.com", phone: "123-456-7890" }, items: [ { productID: "COF001", productName: "Americano", quantity: 2, size: "Medium", options: [ "Extra Shot", "Whipped Cream" ] }, { productID: "PST001", productName: "Chocolate Croissant", quantity: 1 } ], totalPrice: 12.5, pickupTime: "2024-01-05T14:30:00", specialInstructions: "Please make it extra hot." }
✔ success success: message Received - [Topic coffeeshop/order/details/v1/mobile/ca/437/44334], type - TEXT ℹ info info: Message Properties Destination: [Topic coffeeshop/order/details/v1/mobile/ca/437/44334] ℹ info info: Message Payload (bytes): 659
You've now seen the power of publish-subscribe with a Solace event broker. A published event can be delivered to multiple consumers based on the topic subscriptions they have defined.
Tutorial Steps
You've completed the fourth step in the tutorial. Click the link to the next lesson to continue.
Step | Description |
---|---|
|
Set up Your First Event Broker - Complete |
|
Solace EDA Basics - Complete |
|
Set up the Solace Try-Me CLI Tool - Complete |
|
Try a Publish-Subscribe Message Exchange Pattern - Complete |
|
Try a Request-Reply Message Exchange Pattern - Next Lesson |
|
Explore Consumer Scaling Patterns |
|
Next Steps |
You can return to the main Tutorial page at any time.