Acknowledgement on publish - MQTT

2019-08-14 13:31发布

问题:

How to identify whether the publish to a topic was success or not. Is there any way to get acknowledgement on publishing to a topic.

If there is any connection loss during publish between the clients(Publisher/Subscriber) how to handle it.

I don't want the subscriber to send an ack to specific topic in the publishing end after receiving the pay_load.

Here is my ruby code:

Assume, I have created clients(@client) and configured on both sides.

Publish

def publish_it
  @client.publish('test/hai', 'message')
  # Ack the publish
end

Subscribe

@client.subscribe('test/#')

@client.get do |topic,message|
  puts "#{topic}: #{message}"
end

回答1:

There is no end to end (publisher to subscriber) delivery notification in MQTT. This is because as a pub/sub protocol there is no way for a publisher to know how many subscribers there are to a given topic, there could be anything from 0 to n.

The QOS levels built into the spec ensure that messages are delivered from the publisher to the broker (and then from the broker to the subscribers). If you want to ensure a message is delivered then use either QOS level 1 or 2.

QOS 1 will ensure a message is delivered at least once (possibly more if there are network problems)

QOS 2 will ensure a message is delivered only once.

In most of MQTT client libraries there is also the deliveryComplete callback which should be called once all the QOS handshake for a publish has been completed, if you add one of these you can be reasonably confident that the message has made it from the publisher as far as the broker. Unfortunately I can't see this implemented in the Ruby client