moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

Mqtt broker is disconnecting frequently even though it has a unique client id #800

Open venkatesh-chinni opened 4 years ago

venkatesh-chinni commented 4 years ago

Hi I have a mqtt broker up and running. I am connecting to it from python using paho-mqtt. Code

def on_connect(client, userdata, flags, rc):
    if rc==0:
        print("connected OK Returned code=",rc)
    else:
        print("Bad connection Returned code=",rc)

    print("Subscribing to topic","data/#")
    client.subscribe("data/#")

def on_disconnect(client, userdata, rc):
   print("Client Got Disconnected")
   if rc != 0:
       print('Unexpected MQTT disconnection. Will auto-reconnect')

   else:
       print('rc value:' + str(rc))

broker_address="ip"
port = 'port'
print("creating new instance")
client = mqtt.Client(clean_session=True) #create new instance
client.on_connect = on_connect
client.on_message = on_message #attach function to callback
client.on_disconnect = on_disconnect

print("connecting to broker")
client.connect(broker_address, port=port,) #connect to broker
client.loop_forever() #stop the loop

I am using the same code in multiple scripts ,connecting to broker and subscribing to a topic. The frequency of disconnection was less when there were 5-6 scripts. Like 2-3 times an hour. Now, I have around 12-13 scripts connecting to the broker and frequency of disconnects has increased significantly.Its disconnecting once in every 5 minutes. Is there some thing wrong with the connection in the scripts or its about the broker. Is there any limitation on connections a mqtt broker can handle?

Thanks