mqttbrokerhivemqmommqtt-vernemq

How does persistence session work in MQTT?


I use MQTT with QOS 1.

I have a consumer and a producer. They communicate with each other (HiveMQ as a client). They use VerneMQ as a broker. I use a persistence session.

If a consumer is offline and he goes later online, he should become all messages where he was offline. But that works only if consumer is on the other computer and I don't stop a micro service, but I turn off a WiFi and then turn it on. But if I shut down a micro service and then start it again that doesn't work with offline messages.

I think that's why it subscribes again on that topic, if I start a micro service again. Is it the reason? Or not?

UPDATE: I've just tested it without subscribing at the second start of the consumer. That doesn't work either. So subscribing is not the reason why the consumer doesn't get the messages.


Solution

  • For persistence session, so that you become all messages when you were offline, you need some conditions to be satisfied:

    1) turn off CleanStart on connect:

    Mqtt5Connect.builder()
                .cleanStart(false)
                .noSessionExpiry()
                .build()
    

    2) collect remaining messages on connect with publishes

      mqttClient.publishes(MqttGlobalPublishFilter.REMAINING) {
            mqtt5Publish -> handleMessage(mqtt5Publish.topic.toString(), mqtt5Publish.payload.decodeContent())
        }
    

    3) QOS 1+

    That goes like a Swiss watch.