Notes on Python MQTT subscription message QOS=1

QoS0: Send at most once, regardless of whether it reaches the publisher. The publisher (client, server when serving as the sender) only sends once, regardless of whether the receiver receives the data;

QoS1: Arrives at least once. The publisher needs to confirm after arrival. The publisher (when the client or server is used as the sender) publishes the message and waits for the confirmation information from the receiver (when the client or server is used as the receiver). message; if the publisher does not receive a confirmation message, the publisher will keep sending messages;

QoS2: There is only one arrival. The publisher needs to confirm after arrival, and the receiver needs the publisher to confirm again.

According to the three levels of MQTT messages introduced as shown above,

According to my previous understanding, if the message level is 1, then at least one client should be able to receive the message.

But what needs to be noted here is that if no subscription client is connected to the Mqtt server when the message is sent, then no matter how high your message level is, the newly connected subscription client will not receive this message.

The specific scenario is:

(1) I sent a message with qos=1, and then I started a subscription client. At this time, my subscription client could not receive the message.

(2) In order to receive messages with qos=1, the subscribing client needs to use a fixed clientid to log in to the mqtt server and subscribe to a topic. At this time, if the subscribing client is disconnected and messages are sent during the disconnection, then the subscribing client You can receive messages after reconnecting.

The following uses python paho-mqtt as an example

#发送端 设置消息级别是1
mqttclient.publish("topic1", payload="hello world", qos=1)

 

#clean_session=False 是需要设置的参数,表示客户端是一个永久性客户端,短线
#重连后,可以收到错过的消息
scribeclient = mqtt.Client(client_id="clientid11111", clean_session=False)

#连接后订阅的级别同样需要有参数qos=1
self.client.publish(topic, payload=msg, qos=1)

Guess you like

Origin blog.csdn.net/wxtcstt/article/details/126786099