Understanding And Using MQTT v5 Request Response

Web Applications have a direct connection between the sender and receiver.

The sender is the client (web browser) and the receiver is the server.

They implement a command response message flow were the client(browser) makes a request and the server responds and there is a direct connection between client and server.



MQTT uses a publish and subscribe pattern where there is no direct communication between the sending client and the destination client.

Messages are acknowledged but not by the final receiver.

Request-response-diagram-mqttv5

In MQTTv5 the ability to publish a response topic was added in the publish message which allows you to implement the request/response pattern between clients that is common in Web applications.

We can thus create this configuration.

mqtt-server-request-response

In this configuration an MQTT client can function as a traditional server and have other clients connect to it.

The MQTT client (server) could host a database, central logging etc. functioning as in traditional client server networks that are common using http.

Although there still is no direct connection between the client and server(mqtt client) the use of the response topic can create a virtual connection.

How It Works -Client server Illustration with MQTT

Below is the step by step process on how clients can create their own communications channel and function like a tradition client and server.

In the example client1 is the client and makes the request and client2 is the server and responds.

Additional clients can be configured as per client1. These clients all publish on the common topic and receive responses on their own “private” response topic.

Step 1 -Initialisation

Client 1 – The client

Subscribes to it’s response topic e.g. org/responses/client1.

Client 2 – The Server

Subscribes to common topic e.g. org/common. All clients will publish on the topic.

Step2

Client 1 -The Request

Publishes message to common topic e.g. org/common and adds its response topic to the response topic message property of the publish message.

Python example:

client.publish('org/common',"aa",response_topic="org/responses/client1")

Note: Correlation data can be added to the message which will be used to tie the request with the response.

Client 2 -The Response

Receives message on the common topic e.g. org/common and extracts the response topic from the message.

Publishes its response on the response topic along with the the correlation data from the received message if present.

Python example:

client.publish('org/responses/client1',"client2 response message",response_topic="org/responses/client2")

Step 3

Client 1

Receives message on the common topic e.g. org/common and extracts the response topic from the message.

Also extracts the correlation data from the message (if present) and uses it to tie the response to the request.

Client3,4,5 etc

Subscribe to common topic e.g. org/common. Publish on common topic and receive response on their own response topic.

Other Implementation Notes:

Correlation Data

In order for the sender to know what sent message the response refers to it can also send correlation data with the published message.

The receiver e.g. client 2 sends the correlation data back unaltered with the response.

No_local

MQTT v5 has a new property (no_local) when subscribing that tells the broker not to send messages that that client has published.

So when subscribing to the common topic it would be normal to use this feature.

Python example:

client.subscribe('org/common',no_local=True)

Broker Security

You can configure access control lists on the broker so that only client1 can subscribe to the topic org/responses/client1.

Summary

The new response topic feature of MQTTv5 can be used to create a traditional client server architecture that is common in http networks.

This is useful when a tradition server e,g database server needs to be accessed by multiple clients and the responses need to targeted at the requesting client rather than being published to all clients.

Resources

Python Test Scripts

The Python scripts use the gmqtt client available on github and require Python 3.5 and above.

There are three scripts. 2 client scripts and a server script.

Related Tutorials

Please rate? And use Comments to let me know more
[Total: 0    Average: 0/5]

Leave a Reply

Your email address will not be published. Required fields are marked *