I got this code besides other to subscribe a mqtt topic and publish. But sometimes the server disconects. How can I solve this?
def connect_mqtt()-> mqtt_client:
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Conectado NAME_1 MQTT!")
        else:
            print("Error al conectar, código recibido %d\n", rc)
    # Set Connecting Client ID
    client = mqtt_client.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(NAME_2, port)
    return client

client = connect_mqtt()
...
client.loop_forever()