Failed to get more than 20 MQTT messages using Mosquitto / Paho for Python

I am using the MQTT client for Muscovite (now Paho) ​​python to connect to the HiveMQ broker. The code is very simple, taken from their documentation here -

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
    client.subscribe("GB/node0/", 2) 

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print "Topic: ", msg.topic+'\nMessage: '+str(msg.payload)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.


As you noticed in client.subscribe (), the QoS is 2. This is the only modification of the official document.

Publishing client code -

import paho.mqtt.client as mqtt    
mqttc = mqtt.Client("python_pub")
mqttc.connect("", 1883, 60)   
mqttc.publish("GB/node0/", "Hello, World baby!",2) 


Here also the QoS is 2.

This QoS modification results in only 20 messages being received by the subscriber. Upon further testing, I realized that the problem was probably related to max_inflight_messages_set()

, which is an optional function that sets the maximum number of messages with QoS> 0 that can be part of the path through the network stream at once. The default is 20.

However, changing it to any other value does not work. Also, why does the client think these messages are still in sight when they are received? How to solve a problem? How can I be sure that the customer understands that these messages are not "highlighted" and have been delivered?


source to share

2 answers

Try calling mqttc.loop(2,10)

after mqttc.publish()

at the publisher so that the publisher can process the QOS2 confirmation from the broker that it received the publication.

2nd timeout and 10 packets is probably more than needed but it should work



@hardillb is right, you need some form of invocation loop*()

. To be more confident, do the following:

import paho.mqtt.client as mqtt

def on_publish(client, userdata, mid):

mqttc = mqtt.Client() # There normally no need to set a client id.
mqttc.on_publish = on_publish
mqttc.connect("", 1883, 60)
mqttc.publish("GB/node0/", "Hello, World baby!",2)



import paho.mqtt.publish as paho
paho.single("GB/node0/", "Hello, World baby!", qos=2, hostname="")




All Articles