All Products
Search
Document Center

ApsaraMQ for MQTT:Request-response pattern

Last Updated:Aug 23, 2024

ApsaraMQ for MQTT supports the request-response pattern of Message Queuing Telemetry Transport (MQTT) 5.0. This topic describes the request-response pattern.

Feature description

The request-response pattern in MQTT 5.0 is a communication mechanism that allows a client to request a response when the client sends a message. The request-response pattern is implemented in MQTT by using the following new properties: response topic and correlation data.

These two properties are used to correlate a response message to a specific request message. This simplifies the implementation of bidirectional communication.

Scenarios

  • Remote control: You can use the request-response pattern to specify a device to perform a specific operation and return the result. For example, you can remotely control a smart home appliance to switch on or off a light.

  • Request-response interactions: You can use the request-response pattern for service requests and responses. For example, you can initiate a request on the client to obtain the latest weather information and then receive a response from the broker.

  • Remote procedure call (RPC): You can use the request-response pattern to remotely call a backend service and wait for the processing result.

  • Data query: You can use the request-response pattern to initiate a request to query data in a specific dataset on the client and obtain the data response. For example, you can initiate a request to obtain the query result of a database.

Example

You want to query the configurations of an Internet of Things (IoT) device. In this case, you must include the topic that is expected to receive the response and the correlation data that can be selected in the PUBLISH message sent from the client.

// The configurations of the request on the client.
PUBLISH
Topic: requests/device1/getConfig
Response Topic: responses/device1/getConfig
Correlation Data: { "requestId": "abcdef12345" }
Payload: {} // This parameter is left empty, which indicates that the configurations are obtained.
  
// The configurations of the received response.
PUBLISH
Topic: responses/device1/getConfig
Correlation Data: { "requestId": "abcdef12345" }
Payload: { ...configuration data... }

In this example:

  • The client publishes a request message to the topic requests/device1/getConfig.

  • The response topic responses/device1/getConfig indicates that the response is published to the topic.

  • The correlation data {"requestId": "abcdef12345"} is used to match the response to the request.

After the device receives the request, the device publishes the configurations to the response topic and includes the same correlation data in the message. This way, the client can correlate the response with the original request.

With the request-response pattern, MQTT can be used to implement the publish-subscribe pattern and other complex interaction patterns, such as the client-server architecture.