Open pktiuk opened 5 months ago
I reproduce your issue and this should either be fixed in code (preferred) or documented.
I've updated the title, since starting the loop before work and the connection works (cf your reason_code: Success
message) but the subscribe()
is indeed lost. You should be able to see that subscribe()
is lost because it return an error (but no example show error checking).
Regardless of this bug, it might be preferable to subscribe in the on_connect callback to be sure your subscription is kept in case of reconnection (I'm not sure the broker had to persist them, especially when clean_session is True - the default). e.g.
def on_connect(mqttc, obj, flags, reason_code, properties):
print("reason_code: " + str(reason_code))
mqttc.subscribe("/#")
Prerequisites
Note: You may remove this section prior to submitting your report.
A small team of volunteers monitors issues. Please help us to help you by making it simple to understand and, if possible, replicate your issue. Prior to reporting a bug please:
Bug Description
According do docs:
It is acceptable to firstly launch event loop before connecting.
But according to examples (like client_sub ) the order is opposite.
When I tried to start loop before connecting I had problems with connection and with subscription.
Reproduction
def on_connect(mqttc, obj, flags, reason_code, properties): print("reason_code: " + str(reason_code))
def on_message(mqttc, obj, msg): print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
def on_subscribe(mqttc, obj, mid, reason_code_list, properties): print("Subscribed: " + str(mid) + " " + str(reason_code_list))
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.on_subscribe = on_subscribe mqttc.loop_start() ### LOOP STARTED BEFORE CONNECTION mqttc.connect("localhost", 1883, 60) print("Is connected: ", mqttc.is_connected()) mqttc.subscribe("/#")
while True: time.sleep(1)
$ python3 ./example.py Is connected: False reason_code: Success
Fixes the issue:
Environment
Logs