Search code examples
nginxflaskmqttuwsgi

Flask-MQTT disconnects after 'Socket error on client <unknown>' while running on uWSGI NGINX


I have a setup where I use Flask-MQTT to connect my python Flask API to a Mosquitto broker. Whenever I run the Flask API with the development server all is well. But whenever I spin it up for production (using wsgi+nginx), the connection with Mosquitto is made, but everytime i try to publish something i get the following error:

Socket error on client <unknown>, disconnecting.

My app.ini has the processes configured to 1 (processes = 1)

My mosquitto.config has the allow_anonymous flag set to trye (allow_anonymous true)

I can't really seem to figure out what I'm doing wrong here...

Update: So what i think is happening is that the Flask-uwsgi application is trying to connect to mosquitto more than once. There is a master process that connects with Mosquitto on initialize. Then there is a second process that is being used whenever input is given on the Flask app. I'm not sure, but I think Mosquitto only wants one connection at the time, therefor erroring on the second. So now i either need to:

A) Configure Mosquitto in a way that it accepts multiple connection from the same device

B) Configure Flask in a way that wil only use one single process (configuring processes = 1 is not enough, it will still spawn two processes)


Solution

  • Alright, it turns I could've read that the whole multiple processes wouldn't work from the start at the official Flask-MQTT documentation. It sais right there in think letters:

    Flask-MQTT is currently not suitable for the use with multiple worker instances.

    So I looked at my uwsgi app.ini file again closely and actually the answer is quite simple. I turned out i had a like in there master = true.. after I removed that it works like a charm.