Philip Couling
Philip Couling

Reputation: 14924

If an MQTT client disconnects unexpectedly, what messages are lost?

I want to focus on the MQTT standard and less the implementations.

I'm particularly interested in the risk of data loss where a client subscriber disconnects prematurely.

I realise that QOS 0 may be lost. I realise that with clean session set to 1 on connect a client is automatically unsubscribed on disconnect and therefore messages between disconnect and re-subscribe may be lost.

So, assuming a QOS 1 or 2 subscription, and assuming clean session is set to 0 on connect, will the broker just queue up messages until the client returns?

If the client never returns is the broker entitled to abandon the client session (implicitly unsubscribing it)?

Upvotes: 3

Views: 6590

Answers (2)

Wan Chap
Wan Chap

Reputation: 931

http://www.steves-internet-guide.com/mqtt-clean-sessions-example/ Long story short, in order to receive all messages that are published while client is disconnected, you need to:

  1. The client session was connected with clean session set to false
  2. The client had subscribed to the topic with qos>=1
  3. The message is published with qos>=1

The maximum number of message store in broker is broker-dependent. For example in mosquitto, the maximum number of messages can be specified in the config file with max_queued_messages count. Set count to 0 to store unlimited messages (not recommended), the default value for mosquitto is 100 messages.

Upvotes: 4

hardillb
hardillb

Reputation: 59816

It's been a while since I dug that deeply into the v3.x spec (and it has changed for v5 where session expiry is explicit and configurable).

Sessions are supposed to last forever, but brokers such as mosquitto have options such as persistent_client_expiration to say how long it should maintain the queue of messages. These options tend to be added to prevent memory/storage leaks for transient clients.

Upvotes: 3

Related Questions