Search code examples
mqttmoquette

Failed PUBLISH Delivery in QoS1 MQTT


I am using the moquette mqtt broker, and I am trying to understand the implementation as well as the MQTT broker. I hope to make some modifications to the broker for a personal project.

I am curious what should happen when a device sends a PUBLISH msg to the broker, and the broker is unable to deliver the message to the subscribers. The protocol says that a PUBACK is sent back to the publisher. In the moquette source code this PUBACK seems to be sent after forwarding the message to any subscribers.

I commented out the sendPubAck() function to simulate that the message was not successfully published, so I assumed the publisher would publish the message again. However, when I add a print statement next to the incoming message handler function, I only see PINGREQ messages that are sent periodically from the publisher to the broker. I do not see any publish messages.

My question is the following: How exactly does a client device decide when to re-publish a message? Because commenting out the sendPubAck() function doesnt seem to make the publisher resend the message.


Solution

  • There are two choices. Firstly, you could add a message timeout parameter to trigger sending your PUBLISH again if a PUBACK is not received. Secondly, you could resend your PUBLISH only on reconnect.

    I believe the second choice is the best option. The reason for this is down to the possible reasons why the broker (or client of course, depending on the direction of communication) hasn't responded.

    1. You could have a buggy broker, which is effectively what you have created
    2. There could have been a network failure (connection lost but not detected),
    3. The broker could be overloaded.

    For the first case there is nothing we can do, other than get the broker fixed. For the second case, the client must retry the publish when it reconnects. For the third case, sending a duplicate PUBLISH won't help the broker respond, it will just overload it further.

    It's worth noting that the broker should not be waiting for the subscribing clients to respond before sending a PUBACK to the publishing client.