MQTT’s communication model is a pub/sub model. In order to realize Request/Response on the pub/sub model, MQTT v5.0 uses the following mechanism.

Diagram

Client

async_mqtt doesn’t do any special treatment for Request/Response. Simply, send CONNECT packet with RequestResponseInformation property that value is 1, and then receives CONNACK packet. If the CONNACK packet has ResponseInformation, then you can get ResponseInformation. You can subscribe it.

Broker

The broker part of async_mqtt supports Request/Response. When the broker receives CONNECT packet with RequestResponseInformation=1, then the broker automatically generate ResponseInformation and sends back CONNACK packet with it. ResponseInformation is TopicName for the response. So the client can use it as ResponseTopic property when sending PUBLISH packet. async_mqtt broker has authentication/authorization support. The generated topic can be subscribed only the client that sends RequestResponseInformation. The generated topic can be published by all clients.

Correlation Data

The client has only one ResponseTopic and use it for all request. The request receiver client could be different. So the responses could be mixed. In order to distingish correct response, CorrelationData can be used. See the following diagram.

Diagram

client1 got the ResponseInformation R1 and uses it as ResponseTopic. Both client2 and client3 receive the same ResponseTopic and use it for publish back the response. If control PUBLISH packet contains not only ResponseTopic but also CorrelationData, then the receiver just set the CorrelationData to the response PUBLISH packet.

client1 can choose any string as CorrelationData. Typically, choose unpredictable string to avoid malformed response. client1 needs to manage a CorrelationData-Request map to check the corresponding request.