Is there any kind of mosquitto_flush() function?

I need to properly de-initialize the client, ideally ensuring that client, before disconnecting and destroying, will attempt to send all pending publish messages. If there’s no such explicit function like flush, maybe there’s more complicated way to query how many inflight messages are there, and probably implement some timeout function (e.g. if number of messages do not decrease within 10 seconds - forcefully terminate otherwise wait)?

Edit: I put mosquitto_loop(mosq, 5000, 1); but after careful consideration do not think it is the right command to perform the task.

Hi Eugeny,

I think that the best I can suggest for the moment is to check the output of mosquitto_want_write() - this will always return true if there are packets ready for sending. It isn’t foolproof because you may receive a false value if part way through a QoS 2 message flow, for example - packets ready for sending doesn’t mean the same thing as messages ready to send.

An alternative would be to keep track yourself of how many messages you have published and how many have been notified as delivered with the on publish callback.

More generally I think you are correct that the ability to know whether there are pending outgoing messages should exist.

Regards,

Roger

1 Like