eclipse / paho.mqtt.python

paho.mqtt.python
Other
2.15k stars 724 forks source link

How to cleanly disconnect without losing messages? #749

Closed nneul closed 1 week ago

nneul commented 10 months ago

The main doc page has this notation:

QoS 2 messages which have been received from the Server, but have not been completely acknowledged.

Since the client will blindly acknowledge any PUBCOMP (last message of a QoS 2 transaction), it won't hang but will lost this QoS 2 message.

Is there any example of how to do the equivalent of this pseudocode?

connect
loop_forever
send disconnect (in response to some event/change/external indication) - for testing, just assume a disconnect immediately after first message received. 
process any messages that we have received from server, but not yet processed with on_message

Essentially I just want to "process anything pending in receive queue and then exit".

I have tried max_inflight_messages_set(), but that doesn't appear to change any behavior for received messages.

MattBrittan commented 6 months ago

process anything pending in receive queue and then exit

If I'm understanding you correctly then this is not something the MQTT protocol really supports (there is no way to ask the server to send all waiting messages and then disconnect). All you can really do is connect and wait until you believe you have all of the messages (possibly due to a timestamp within the message body, or timing out when, for example, no messages are received in a second).

max_inflight_messages_set() relates to outbound messages (and only relates to the connection between the client and the server).

If the above answers your question then please close off this issue, otherwise please provide further information re your requirements (but, perhaps, stackoverflow might be a better venue as this seems likely to be a protocol question rather than something specific to this library).

MattBrittan commented 1 week ago

Closing as I believe that this was a question which has been answered.