moluzhui
moluzhui

Reputation: 1093

keepalive set paho.mqtt simple API automatic disconnect

I want to code a synchronous programs in which cloud send mqtt message to device, then using simple to wait response to judge whether is successed. but it need a timeout, such as 5 seconds, app think it's failed. The keepalive parameter of mqtt simple API seems to lose efficacy, but the big probability is that I use or understand the error.

I would very appreciate it if you guys can suggest me some advice

print("----before simple")
    msg = subscribe.simple("paho/test/simple", hostname="39.100.79.76",port=1883,keepalive=5,will = {'topic': "paho/test/disconnect", 'payload':"network or device anomaly", 'qos':2, 'retain':0})
    print("----after simple")

then run it, simple API cannot to end

----before simple
infinite...

Correctly determine if it is successful to synchronize the edge cloud application

Upvotes: 1

Views: 1722

Answers (1)

hardillb
hardillb

Reputation: 59731

You have miss understood what the keepalive property for a MQTT client is for.

The keepalive is used by the Broker to check if the client is still functioning. It does this by keeping a timer since the last MQTT packet was received from the client. If it does not receive a packet when the timer reaches the keepalive time it sends a MQTT Ping request to the client. If it doesn't receive a response to that packet with in half the keepalive time then is will disconnect the client and publish any Last Will & Testament message that the client may have set.

The Paho client library handles MQTT Ping messages in the background with no need for the user to be involved.

The code sample you have provided will wait indefinitely for a response.

Upvotes: 1

Related Questions