MQTT qos parameter has no effect

五迷三道 提交于 2020-01-25 11:57:07

问题


I have installed a mosquitto server on a raspberry server.

This server works fine: I have test with mosquitto_sub and mosquitto_pub commands.

I have write this python script:

import paho.mqtt.client as mqtt
import time

client = mqtt.Client('module_test_4')
client.connect('127.0.0.1', 1883, 10)

client.loop_start()


for i in range(10):
   client.publish('topic_2', "valeur %d" % i, qos=0)
   time.sleep(1)

client.loop_stop()
client.disconnect()

I have launched this script twice on 2 consoles:

 mosquitto_sub -h 127.0.0.1 -i module_test_2 -t topic_2

It works fine: I see messages on each console.

Now, i have tried to change qos parameter to 0,1 and 2.

I have tried to run my python script without lauching any occurence of mosquitto_sub.

I was thinking mosquitto will buffer messages and send it again when mosquitto_sub will be launched but this does not work.

So i am wondering how qos works...

Thanks


回答1:


QOS only applies to one leg of the connection at a time.

This means the QOS can be different between the publisher/broker and broker/subscriber.

So in the example you have posted you set QOS to 2 between the publisher and the broker, but it is still the default 0 between the subscriber and the broker. This means that as far as the broker is concerned the subscribing client only wants QOS 0.

If you want to test with mosquitto_sub you need to include a higher QOS on the command line as well. You need to have established a subscription at QOS 2 before disconnecting like so:

mosquitto_sub -h 127.0.0.1 -i module_test_2 -t topic_2 -q 2

You also need to tell mosquitto_sub not to ask for a clean session when it reconnects:

 mosquitto_sub -h 127.0.0.1 -i module_test_2 -t topic_2 -q 2 -c 


来源:https://stackoverflow.com/questions/58639252/mqtt-qos-parameter-has-no-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!