2

I'm trying to build an instant-messaging app using MQTT. But I've hit a road block as I'm not able to receive all messages sent by the publisher when the client reconnects after going offline for some time. The client is connected to the broker with these settings:

  1. A client id
  2. clean session - false
  3. receive with QoS 2

While the publisher sends messages with these settings:

  1. QoS 2
  2. retain flag set to true

The problem is when the client reconnects, it receives only the latest (offline) message sent by the publisher while all the preceding messages are lost.

I was going through some articles where it is mentioned that the persistent connection means that the broker persists the topic subscriptions and all the QoS 1 and 2 messages. Here are some of them: HiveMQ persistent connections, another article.

Is there a workaround wherein I can get all the messages published on a topic while the client was offline or I am doing something wrong?

P.S. I've gone through this Receive offline messages mqtt link already and I'm doing the same as answered but it doesn't solve my issue.

Rahul Sharma
  • 5,562
  • 4
  • 24
  • 48
  • Just to be 100% clear when the subscriber reconnects it needs to connect with the SAME client id as it used before. It's not clear from what you've written that it's the same client id – hardillb Sep 21 '18 at 09:21
  • Also which broker you using and how long between disconnect and reconnect – hardillb Sep 21 '18 at 09:25
  • Yes the client id remains same after the client reconnects. The difference between the disconnection and reconnection is just a few seconds since I'm just testing the offline messages. The broker I'm using is mosquitto. – Rahul Sharma Sep 21 '18 at 09:33
  • Then you must be doing something wrong when setting up the client, edit the question to sure your code – hardillb Sep 22 '18 at 07:24
  • @hardillb, there was actually something wrong with the client initialisation (in my code). I was using the wrong method of Paho client. I was trying to test the behaviour using mqtt.fx and mqttbox and was not able to receive the offline messages. Everything works fine now. Thanks. – Rahul Sharma Sep 26 '18 at 10:01
  • 2
    Mind posting your solution as an answer? – Ojonugwa Jude Ochalifu May 09 '19 at 01:59

1 Answers1

0

MQTT retain flag ensures that only the last well known message is stored in the broker. Disabling the retain flag and having a persistent client connection will enable queuing of messages in the broker (only with qos 1 and 2) and delivering the same when client comes back online. please keep in mind to use the same client id on reconnection to broker since broker maintains the context of the client using the client id.