In our scenario we want to get the last values of all the topics when you first connect (or with a clean session). This is of course a perfect candidate to just have all the messages with retained(true).
However, when reading the message expiry setting. I see: A retained message that is stored on the broker expires.
Am I right in assuming that this setting will remove retained messages that are older than the message expiry setting? If so, that is something that we do not want, so we could solve it by setting the message expiry to a very large number. However, does that mean that it will keep all messages. So not just the messages that are retained? That would imply a massive amount of messages that would get saved right?
Yes, your understanding is mostly correct. The implications of setting a very large expiry interval would effectively prevent retained messages from expiring, but this setting applies individually to each message that has the interval set. It doesn’t mean the broker will keep all messages indefinitely only those with the expiry interval set.
For MQTT version 3 clients, the specification of the publish message expiry interval individually is not an option. In such cases, the broker-side setting is applied, as detailed here: HiveMQ Configuration :: HiveMQ Documentation.
For MQTT version 5 clients, there is the flexibility to specify the message expiry interval individually for each PUBLISH message. This feature allows you to set a longer interval for retained messages and a shorter interval for other messages as needed.
However, in cases where accommodating MQTT v.3 clients becomes imperative, a custom HiveMQ Extension can be created, using HiveMQ Extension SDK. By doing so, it becomes possible to intercept all incoming MQTT v.3 publish packets and add a message expiry interval setting to them: Interceptors :: HiveMQ Documentation
Concerning the potential accumulation of messages with a longer message expiry interval for retained messages, rest assured that only the last retained message per topic is stored.
I hope it helps. Should you have further questions please don’t hesitate to reach out.