Request/Response Pattern Over MQTT

linda Examples 2 Comments

MQTT is a lightweight messaging protocol that is based on publish/subscribe pattern. Due to its low overhead and simplicity, this protocol is suitable for use in IoT and M2M applications.

In this post, we show you how easy it can be with Reactive Blocks to utilize also a request/reponse pattern in MQTT.

Let’s take a look at an example of a vehicle tracking system. A vehicle, e.g., shipping truck, periodically reports its current location to a backend system. In this case, an MQTT client running on the truck periodically publishes messages containing its position with a particular topic to an MQTT broker. On the backend, another client subscribes to that topic and gets updated by the broker when new messages come. We have developed building blocks for MQTT that can help you creating such application. See MQTT in action in this publisher and subscriber examples (sign in required).


What about the request/response pattern?

Another messaging pattern that is often used is request/response. For instance, in our tracking system, the truck breaks down and the driver cannot fix the problem. Hence, the driver sends a request for assistance and the backend can, for example, dispatch a mechanic nearest to the truck’s location. This interaction is of type request/response.

Since we are already using MQTT in our application, can’t we just use MQTT also for the request/response pattern? The answer is YES, but we need to carefully design the topic structure in order to connect the response to a particular request.


Request/Respond MQTT building blocks

We expand our MQTT library with the Requester and Responder blocks. These blocks take care of the correlation between a request and a response. When using these block, you just need to focus on your applications. That is, which message to send (on the requester side) and what is the response upon receiving the message (on the responder side). The two blocks also need to agree on a base topic. For our example, this base topic can be assistance. Once these details are determined, it is pretty straightforward to use these new MQTT blocks, as illustrated in the following figure.


  1. When the driver presses the help button, an event ASSISTANCE_REQUIRED is triggering the creation of a String to be sent by the MQTT Requester block.
  2. On the backend, the String is received via pin request of the MQTT Responder block. Thereafter, a process, e.g., dispatching a mechanic, occurs and a response String is generated.
  3. The response is sent by the Responder block.
  4. Later, the response String is received by the truck driver.
  5. If there is no response within a few seconds or an error occurs, the MQTT Requester will emit a signal via pin failed.

The base topic, broker address, as well as timeout duration on the requester are configurable as instance parameters.

How does it work?

Internally, the base topic is used to correlate request and response. The requester first subscribes to a topic to which the response will be sent. With the given base topic, this response topic will be assistance/response/ID, where ID is a unique identifier for the client request. The responder subscribes to the topic assistance/request/+, where + is a wildcard for a topic level. The requester publishes its message to the request topic, which is composed from assistance/request/ followed the same unique ID already used for the response topic. The responder will receive this message, since the unique ID matches the wildcard. When the responder receives the message, it will extract the unique ID and send a response to the proper response topic.

To obtain these blocks, simply update your imported library if you already have the MQTT library. New to Reactive Blocks? Install and try this measurement to cloud reference system. Don’t hesitate to contact me for any questions or comments :-).


Comments 2

  1. Pingback: Consuming Mobile APIs with MQTT Req/Res pattern.

  2. Post

Leave a Reply