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:

Diagram illustrating the concepts described in the surrounding text

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:

Animation showing a publish-subscribe message exchange

To set up your applications and publish an event, perform these steps:

  1. Open four command-line terminal windows, one for each application.
  2. In terminal 1, subscribe the Barista Station application to all events from the country ca from all source types. Enter the following command:
  3. 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/>
  4. In terminal 2, subscribe the Kitchen Floor application to all events from the country ca with from all source types. Enter the following command:
  5. 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/>
  6. In terminal 3, subscribe the Human Resources application to all staff arrival events in the country ca. Enter the following command:
  7. 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/>
  8. 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."
    }
  9. 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...

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:

  1. Check the Barista Station application in terminal 1. The following message should appear: 
  2. ✔  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."
    }
  3. Check the Kitchen Floor application in terminal 2. The following message should appear:
  4. ✔  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
  5. Check the Human Resources application in terminal 3. No message should appear because the subscription does not match the topic of the published message.
  6. Press Control+C (on Windows and Linux) or Command+C (on Mac), to stop the applications.

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

Step 1

Set up Your First Event Broker - Complete

Step 2

Solace EDA Basics - Complete

Step 3

Set up the Solace Try-Me CLI Tool - Complete

Step 4

Try a Publish-Subscribe Message Exchange Pattern - Complete

Step 5

Try a Request-Reply Message Exchange Pattern - Next Lesson

Step 6

Explore Consumer Scaling Patterns

Next Steps

You can return to the main Tutorial page at any time.