MQTT: what is and how does the "retain" work

2 minutes of reading

MQTT logo

La retain is yes among the most important features of the proProtocol MQTT, but also among the most unknown, which often causes many proproblems to the correct functionnameof the profirst personal home automation.

In the MQTT area, as sappLet's go, every client it has no way to obtain certainty with respect to the delivery of a given message to the recipient, but only - at most - of the correct delivery to the broker (to fill in any gaps we recommend reading of the card dedicated to QoS). By the same logic, a client that subscribes to a topic of interest at the broker will not be able to know when (and if) another client will publish a message on this topic. He is totally in the dark.

And this is where the concept of retain.

How it works

Each message sent to the broker by an MQTT client has one flag di retain that can be evaluated "true"Or"false”Based on the fact that the client has activated o not activated, for that message, this function.

Where the flag is set to "true" (so, in jargon, both activated the retain), this message - in addition to being delivered immediately to the connected clients who have subscribed to the topic indicated in the message itself - it is put aside, kept so to speak, by the broker, waiting to send it to any client that connects at a later time and that subscribes to the topic indicated in the message.

Let's take an example that the MQTT client present on a home automation actuator Sonoff riprogrammato with firmware Tasmota send MQTT telemetry messages relating to probefore operating status ("on" / "off") with retain not active and we also propose to own a domotics based on HUB personal that has the profirst client registered to the topic indicated in the telemetry messages above in order to collect the status of the actuator for the purposes propri of personal domotics.

Let's imagine that theHUB reboot (or lose connection, for different reasons): upon completion of the reboot the actuator mentioned above would be near theHUB in a state sconosciuto until the actuator for some reason sends a new status message to the broker.

If, on the other hand, the telemetry message sent by the actuator has activated the active retain, the reconnection / subscribe to the topic by the client of theHUB would cause immediate sending to it of the last message saved by the broker, so as to immediately have the status (at least the last received) available.

When to use it

Simple: in principle the least possible. The reason is simple: unless it is necessary at all costs receiving a message - also procrastinato - by the recipient, the presence of the active retain can create more proproblems of those who do not solve it.

Let's consider the case of actuators equipped with MQTT support: sometimes such devices happen they lose the connection Wi-Fi. It happens: these are devices with small antennas, sometimes the same firmware has small ones proproblems that cause scattered disconnections. The use of commands sent from home automation with active retain, for example, sometimes causes more prowhat else: let's imagine we sent an ON command to the actuator via home automation, and then set it manually to OFF (with a physical switch, maybe) while, for a few moments, the actuator was offline. At the first reconnection the actuator is useful would rekindle "alone" (because it would receive a command sent by the broker due to the active retain), creating an unintended situation.

Advice

It is therefore advisable not to activate may the retain except for very specific cases in which it is really necessary, at all costs, the delivery of a specific message to specific recipients. Furthermore, we recommend - after verification of the proyour configurations - to clean any retainers on the profirst broker.


telegram

Stay up to date through ours Telegram channel!