0

I have below code for mqtt client for receiving message from broker. If client is disconnected from broker then client tries to connect with broker using connect() call again. I have read paho documentation saying that loop_start() will handle reconnection with broker. Please let me know if is it right to use connect() call for reconnection with broker or let it handle by loop_start() itself.

import paho.mqtt.client as mqtt
import json
import time

is_connect = False

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print("Message received on "+msg.topic)
    parsed_message = json.loads(msg.payload)
    print(json.dumps(parsed_message, indent=4, sort_keys=True))

def on_disconnect(client, userdata, rc):

    if rc != 0:

        print "Unexpected disconnection." , (rc)
        global is_connect
        is_connect = False
while True:
    global is_connect
    #If client is not connected, initiate connection again 
    if not is_connect:
        try:
            client = mqtt.Client(client_id='testing_purpose', clean_session=False)
            client.loop_stop()
            client.on_connect = on_connect
            client.on_message = on_message
            client.on_disconnect = on_disconnect
            client.connect("localhost", 1883, 5)
            is_connect = True
            client.loop_start()
            time.sleep(15)
        except Exception as err:
            is_connect = False


# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
#client.loop_forever()
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
cgoma
  • 47
  • 2
  • 13
  • There doesn't seam to be an actual question here. Have you tried this code to see what happens? – hardillb Oct 15 '17 at 08:37
  • Having said that, you might want to move the `is_connect = True` to in the `on_connect` method – hardillb Oct 15 '17 at 08:38
  • @hardillb This code works fine. But I have one doubt that I am creating object of mqtt client on every disconnect call. Do it will create any issues with mqtt broker. Also i will move is_connect = True to on_connect() – cgoma Oct 16 '17 at 13:19

1 Answers1

0

OK, so the Paho client only reconnects if you use the loop_forever function.

So rather than tearing down the whole connection you can always use the reconnect function to reconnect if you want to have control of the network loop. But the simplest way is to just use the loop_forever function as follows:

import paho.mqtt.client as mqtt
import json

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print("Message received on "+msg.topic)
    parsed_message = json.loads(msg.payload)
    print(json.dumps(parsed_message, indent=4, sort_keys=True))

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print "Unexpected disconnection." , (rc)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client = mqtt.Client(client_id='testing_purpose', clean_session=False)
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.loop_forever()
hardillb
  • 54,545
  • 11
  • 67
  • 105