Rahul Sharma
Rahul Sharma

Reputation: 5995

Get all messages after the client has re-connected to the mqtt broker

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.

Upvotes: 2

Views: 2260

Answers (1)

santosh
santosh

Reputation: 1

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.

Upvotes: 0

Related Questions