All Products
Search
Document Center

IoT Platform:Request-response pattern

Last Updated:Jun 21, 2023

This topic describes the request-response pattern in Message Queuing Telemetry Transport (MQTT) 5.0 supported by IoT Platform.

Background information

In the publish-subscribe pattern of MQTT, messages are asynchronously published and received, and the publisher and the subscriber are decoupled. Some actual business scenarios of IoT devices require responses from subscribers. In this case, the request-response pattern can be used. For example, a server sends a command to turn on or turn off a device, or a device requests data from a server.

Feature description

The request-response pattern in MQTT 5.0 uses asynchronous request and response messages, which is different from the HTTP request-response pattern. To associate the response message with the request message, Response Topic and Correlation Data are added to the request message. This simplifies development on the responder.

  • Response Topic: a string that specifies the topic to which the responder publishes the response message.

  • Correlation Data: binary data that stores the context of the request. The responder can use the data to identify the request message.

The following figure shows the messaging process when the request-response pattern is used.

image..png
Important
  • When you send multiple requests to a device at the same time, you can include a request ID in Correlation Data of each request message. This allows the responder to identify the requests.

  • IoT Platform supports the revert-remote procedure call (RRPC) communication. If you want to control remote devices in real time, you can use RRPC to synchronously obtain responses.

  • QoS 1 messages must be received by IoT Platform. If you want to ensure that subscribers can receive messages and complete actions that are required in the messages, we recommend that you use the request-response pattern.

Scenarios

A device requests data from the server

In some scenarios, IoT devices need to request data from the server. For example, a smart cooking machine requests various recipes from the cloud, and different types of recipes are stored in separate topics. If the request-response pattern is used, the server only needs to respond with recipes without the need to distinguish topics. This simplifies development on the server.

  • The device can use Correlation Data to associate the request and response messages, which is similar to the remote control scenario.

  • The server only responds with the data requested by the device, and does not identify the topic to which the response message belongs.

image..png

The server controls a remote device

When a server sends a command to a remote device, the server expects to receive a response immediately. For example, a server sends a command to unlock an electronic lock. The request-response pattern can be used in this scenario, and you can include the device identity information to Correlation Data to authenticate the device.

image..png