-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flask MQTT on_connect is never called when used with SocketIO #82
Comments
I'm having this same problem. I even modified the source through this function here: def _handle_connect(self, client, userdata, flags, rc):
# type: (Client, Any, Dict, int) -> None
if rc == MQTT_ERR_SUCCESS:
self.connected = True
for key, item in self.topics.items():
self.client.subscribe(topic=item.topic, qos=item.qos)
if self._connect_handler is not None:
logger.debug("Internally handling MQTT connection...\nPassing off to user handler: " + str(self._connect_handler))
self._connect_handler(client, userdata, flags, rc) This function is Here is what my callback looks like: @mqtt.on_connect()
def mqtt_connect_cb(client, userdata, flags, rc):
app.logger.debug("MQTT Connected!")
... And here is my output:
My callback is registered correctly and I would expect to see EDIT: I should also note that this problem happens when I'm creating my application through the Factory method. When I was tinkering with Flask-MQTT + Socketio in a single test file, both libraries were able to work properly. |
Can you please elaborate on "Flask-MQTT + Socketio in a single test file.. work properly" ? Please see my sample code here: |
My single file test looks similar to yours except I initiated the SocketIO object without the I'm running the server through eventlet |
I'm also having the same issue where on_connect is never called, so therefor I can't automatically subscribe my default topics. The main issue seems to be until you establish the initial connection to Flask from the browser then socketio threads don't start. I also tried with before_first_request but that also required an initial request to start the main threads. My use case is I am trying to run a flask + mqtt + socketio site, where flask and mqtt should start as background processes and subscribe to the topics I want to and process those events even if a browser has never connected. Would love to see this bug fixed. |
when i simply added call to mqtt.subscribe() just before socketio.run() it works. |
@Osmiogrzesznik, mqtt.subscribe() works for me, and I am able to receive messages. But the issue is when to subscribe. I want to do it whenever a connection is established, so that even if reconnects, my subscription is renewed automatically. But the life cycle event mqtt.on_connect() is never invoked. |
I have the same problem. |
Let me look into this. Will get back to you soon :) |
@urbanskalar can you elaborate your problem? |
I created a gist here: https://gist.github.com/Sohaib90/07a616c0dfe8e8d4eab24d2d60ece2b1, which follows the same pattern as https://stackoverflow.com/questions/64592277/flask-mqtt-on-connect-is-never-called-when-used-with-socketio except that I do not use In my gist, on line 58, I print "Connected to MQTT broker" and proceed to subscribe to a test topic and also print that to get this result: You can see here that the call is invoked and I do get to subscribe to the Unless I am doing something wrong here, shouldnt this be the expected outcome and that the callback is being invoked? |
@Sohaib90, I see that you are subscribing to MQTT on socketio.on('subscribe') also. This is probably a client-initiated call. Can you please disable this and try again?
The only other difference I can think of is the way gevent and eventlet handle background tasks. You are monkey-patching eventlet, so this may have eliminated some underlying problem in async mode compatibility. I am only guessing here. Somebody familiar with Flask-MQTT source code can throw light on this. |
Hey sorry for late reply. I kind of moved on from this problem now, so I might give you some inaccurate info. Basically, when I first tried to implement this, I wasn't aware of the example here. I was only following the documentation. At that time I was still using app.run() instead of socketio.run(). Also my code didn't include lines eventlet.monkey_patch(), socketio = SocketIO (app) and bootstrap = Bootstrap(app). I still don't understand why it didn't worked without that. At some point I started thinking maybe I am doing something wrong. |
Call me crazy but if you init socketio before mqtt it works 🤷 Works:
Does not work:
Not sure why this is working for me? |
Also ran into this issue. I had not SocketIO, but Influx between call to flask_mqtt.Mqtt(app) and on_connect callback. Looks like any delay can mess things up:
My limited understanding tells me that |
I am trying to implement an MQTT to Web Socket bridge on the lines of your helpful example
https://flask-mqtt.readthedocs.io/en/latest/usage.html#interact-with-socketio
In the above example, subscribing to the MQTT topic is triggered by the socket client. But I want my MQTT channel to keep communicating even if there is no socket client.
So I tried to subscribe in the @mqtt.on_connect() event. But that callback is never invoked. But once subscription is initiated using a socket, MQTT messages start flowing in all right.
The plain Flask-MQTT example works fine. The callback fails when Socket support is added to it. So I am curious to know how async_mode and socketio.run() interact with the MQTT life cycle events. Especially since multiple workers are not supported.
I have posted a minimum reproducible sample code here:
https://stackoverflow.com/questions/64592277/flask-mqtt-on-connect-is-never-called-when-used-with-socketio
The text was updated successfully, but these errors were encountered: