0

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.

Simson
  • 3,373
  • 2
  • 24
  • 38
JetBrains
  • 456
  • 2
  • 6
  • 18
  • QOS 1, it would not work with QOS 0. – JetBrains Nov 01 '19 at 06:16
  • It's not supposed to work at QOS 0 – hardillb Nov 01 '19 at 07:18
  • Sure, but I Use QoS 1. – JetBrains Nov 01 '19 at 07:32
  • That's not what you said in the lasts comment, that implied it worked with QOS1 but not with QOS0. But also WHERE are you using what QOS? https://stackoverflow.com/questions/58639252/mqtt-qos-parameter-has-no-effect/58639712#58639712 – hardillb Nov 01 '19 at 07:35
  • I use QOS 1 for both: consumer to broker - connection, and producer to broker - connection. That works if I don't stop a micro service, but I turn off a WiFi and then turn it on. But if I stop a micro service and then start it again, that doesn't work. By starting it must connect again. Maybe that is the reason. – JetBrains Nov 01 '19 at 09:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201711/discussion-between-jetbrains-and-hardillb). – JetBrains Nov 01 '19 at 09:04
  • here you can find explanations with examples about persistance and QoS. http://www.steves-internet-guide.com/mqtt-clean-sessions-example/ Check Retain Messages, Clean Session and QOS table to understand better how things works. – idan Nov 01 '19 at 12:38

1 Answers1

1

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.

JetBrains
  • 456
  • 2
  • 6
  • 18
  • With version 1.2.0 of the HiveMQ MQTT Client you can set up your subscriptions before connecting the client which removes the necessity for consuming remaining publishes (you are right that this was needed before version 1.2.0). – SgtSilvio Apr 22 '20 at 20:10