Step 5: Try a Request-Reply Message Exchange Pattern

You've now completed Step 4: Try a Publish-Subscribe / Fan-Out Message Exchange Pattern.

In step 5, you'll expand your EDA by setting up a request-reply message exchange.

With request-reply messaging, applications achieve two-way communication using separate point-to-point channels: one for requests, and another for replies. In a request-reply pattern, the applications are both producers and consumers.

diagram illustrating the concepts described in the surrounding text

In the Solly's Coffee OMS we introduced in step 3, a Third-Party App sends a new order event as a request to the Order Management application whenever a user places an order. Upon receiving the request, the Order Management application replies with the order-id in the response. This interaction uses a request-reply message exchange, ensuring that the order confirmation is received synchronously.

Set Up a Request-Reply Message Exchange

In this exercise we'll set up two applications, Order Management and Third-Party App. After you complete these steps you'll have a message exchange pattern like the following animation:

Animation showing a request-reply message exchange

To set up a request-reply message exchange, perform these steps:

  1. Open two command-line terminal windows, one for each application.
  2. In terminal 1, set up the Order Management application with a subscription to the topic, coffeeshop/order/details/v1/*/ca/> that replies to requests with a payload containing a status and orderId. { status: 'Accepted', orderID: '12738' }. Enter the following command:
  3. stm reply --topic "coffeeshop/order/new/v1/*/ca/>" --client-name "Order Management" --message "{status: 'Accepted', orderId: '12738'}"

    You should see the following result:

    ℹ  info      info: loading 'reply' 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 subscribe to request topic. ===
    ℹ  info      info: subscribing to coffeeshop/order/new/v1/*/ca/>
    ℹ  info      info: press Ctrl-C to exit
    ✔  success   success: successfully subscribed to request topic: coffeeshop/order/new/v1/*/ca/>
    …  waiting   === ready to receive requests. ===
  4. In terminal 2, set up the Third-Party App to send a request message with the topic, coffeeshop/order/new/v1/mobile/ca/437/4334. The application waits for a default time of 10 seconds to receive a reply before generating a time-out error.
  5. stm request --topic "coffeeshop/order/new/v1/mobile/ca/437/4334" --client-name "Third Party App" --file order.json

    You should see the following result:

    ℹ  info      info: loading 'request' command from configuration 'stm-cli-config.json'
    …  waiting   connecting to broker [ws://localhost:8008, vpn: default, username: default, password: ******]
    ℹ  info      info: client name: Third Party App
    ✔  success   success: === Third Party App successfully connected and ready to send requests. ===
    …  waiting   requesting...
    ✔  success   success: request sent on topic coffeeshop/order/new/v1/mobile/ca/437/4334
    ℹ  info      info: Message Properties
    Destination:                            [Topic coffeeshop/order/new/v1/mobile/ca/437/4334]
    Class Of Service:                       COS1
    DeliveryMode:                           DIRECT
    ℹ  info      info: Message Payload (bytes): 659
    ✔  success   success: reply Received - [Topic coffeeshop/order/new/v1/mobile/ca/437/4334], type - TEXT
    ℹ  info      info: Message Properties
    Destination:                            [Topic #P2P/v:solbroker/mPuoLl8m/3rd Party App/_]
    CorrelationId:                          #REQ1
    Class Of Service:                       COS1
    DeliveryMode:                           DIRECT
    Reply Message
    Binary Attachment:                      len=41
    ℹ  info      info: Message Payload (bytes): 38
    ✔  success   success: disconnecting from Solace PubSub+ Event Broker...
    ✔  success   success: disconnected.
    ✔  success   success: exiting...

    Third Party App sends a request message to Order Management with a new order. Order Management replies to the request with a message of { status: 'Accepted', orderID: '12738' }.

  6. Press Control+C (on Windows and Linux) or Command+C (on Mac), to stop the applications.

Tutorial Steps

You've completed the fifth step in the tutorial. Click Next Steps for suggestions to continue your Solace journey.

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 - Complete

Step 6

Explore Consumer Scaling Patterns - Next Lesson

Next Steps

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