Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to receive more than 20 MQTT messages using Mosquitto/Paho for Python

I'm using the Mosquitto (now Paho) python MQTT client to connect to a HiveMQ broker. The code is very basic, as taken from their documentation here - https://pypi.python.org/pypi/paho-mqtt

#SUBSCRIBER
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("192.168.15.4", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
client.loop_forever()

As you notice in client.subscribe(), the QoS is 2. This is the only modification to the official documentation's code.

The publishing client's code is-

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

Here also, the QoS is 2.

This modification of QoS results in only 20 messages being received by the subscriber. Upon further probing, I realized the problem is probably due to max_inflight_messages_set(), which is an option function which sets the maximum number of messages with QoS>0 that can be part way through their network flow 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 inflight when they've been received? How do I resolve the problem? How do I ensure that the client understands these messages are not "inflight" and have been delivered?

like image 700
sbhatla Avatar asked Sep 12 '14 19:09

sbhatla


2 Answers

Try calling mqttc.loop(2,10) after the mqttc.publish() in the publisher so the publisher can handle the QOS2 acknowledgement from the broker that it has received the publish.

The 2 second timeout and the 10 packets is probably more than is needed but it should work

like image 72
hardillb Avatar answered Nov 14 '22 23:11

hardillb


@hardillb is right, you need some form of loop*() call. To be more certain, do this:

import paho.mqtt.client as mqtt

def on_publish(client, userdata, mid):
    client.disconnect()

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

Or:

import paho.mqtt.publish as paho
paho.single("GB/node0/", "Hello, World baby!", qos=2, hostname="192.168.15.4")
like image 20
ralight Avatar answered Nov 14 '22 22:11

ralight