pythonflaskmqttmosquittoflask-socketio

flask-mqtt: Mqtt Client disconnects immediately after connect


i tried the example project of the Flask-MQTT (https://github.com/stlehmann/Flask-MQTT) with my local mosquitto broker. But unfortunatly it is not working. Subscription and publish are not forwared correctly. so i've added some logger messages:

def handle_connect(client, userdata, flags, rc):
print("CLIENT CONNECTED")

@mqtt.on_disconnect()
def handle_disconnect():
print("CLIENT DISCONNECTED")

@mqtt.on_log()
def handle_logging(client, userdata, level, buf):
print(level, buf)

16 Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k30) client_id=b'flask_mqtt'
CLIENT DISCONNECTED
16 Received CONNACK (0, 0)
CLIENT CONNECTED
16 Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k30) client_id=b'flask_mqtt'
CLIENT DISCONNECTED
16 Received CONNACK (0, 0)

The mosquitto Broker shows that it disconnects the flask app because of the client is already connected:

1580163250: New connection from 127.0.0.1 on port 1883.
1580163250: Client flask_mqtt already connected, closing old connection.
1580163250: New client connected from 127.0.0.1 as flask_mqtt (p2, c1, k30).
1580163250: No will message specified.
1580163250: Sending CONNACK to flask_mqtt (0, 0)
1580163251: New connection from 127.0.0.1 on port 1883.
1580163251: Client flask_mqtt already connected, closing old connection.
1580163251: New client connected from 127.0.0.1 as flask_mqtt (p2, c1, k30).
1580163251: No will message specified.
1580163251: Sending CONNACK to flask_mqtt (0, 0)
1580163251: Socket error on client flask_mqtt, disconnecting.

i also tested a simple python.paho mqtt client example without flask and it works as expected. i also changed tried several loop starts inside the flask-mqtt code self.client.loop_start() --> self.client.loop_forever() ... did not change anything.

so any idea where's the problem? i also debugged the flask-mqtt code and cannot find issues. (my python version is Python 3.6.9 (default, Nov 7 2019, 10:44:02) (my host system is elementary Linux)

maybe the FLASK-MQTT lib is deprecated ? any hint or idea is appreciated!


Solution

  • The reason this is failing is in the mosquitto logs.

    1580163250: New connection from 127.0.0.1 on port 1883.
    1580163250: Client flask_mqtt already connected, closing old connection.
    1580163250: New client connected from 127.0.0.1 as flask_mqtt (p2, c1, k30).
    1580163250: No will message specified.
    1580163250: Sending CONNACK to flask_mqtt (0, 0)
    

    Every client that connects to the broker must have a unique client id. In this case the flask client is trying to make multiple connections to the broker with the same client id. When the second connection starts, the broker sees that client id is the same and automatically disconnects the first.

    You've not actually supplied any code showing how you are settings up the client connections so we can't make any suggestions on how to actually fix it. Did you pay attention to the comment at the end of the last example in the README.md on the github page?