I'm writing a MQTT client which simply connects to the broker, publish a message and then disconnect. Here's the code:
def on_connect_v5(client, userdata, flags, rc, properties):
print('connected')
client.publish(topic, payload, 0)
def on_publish(client, userdata, mid):
print(f'mid: {mid}')
client.disconnect()
client = paho.Client(protocol=paho.MQTTv5)
client.on_connect = on_connect_v5
client.on_publish = on_publish
client.connect(host, port, 60)
client.loop_start()
# client.loop_forever()
The question is when I use loop_start()
, it seems the client isn't connected successfully, but loop_forever()
would work. Have I done something wrong with the loop_start()
function, and what's the proper way to use it?
BTW: have tried use the
paho.mqtt.publish
module and always get a Socket timed out. Appreciated if someone can explain it as well.