Skip to content
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

"Connect" Event not firing when flask server running on Raspberry Pi #731

Closed
Rory-Wade opened this issue Jul 7, 2018 · 3 comments
Closed

Comments

@Rory-Wade
Copy link

Rory-Wade commented Jul 7, 2018

I have two identical builds of a flask applications running default servers on a Mac OSX and Raspberry Pi environments. In the Mac environment the flask application is able to respond to a socket connection and send data back to the client. However, on the Raspberry Pi the client shows a socket connection in the networking tab of the debug logs but no real data is being received from the server because it never runs
@socketio.on('connect', namespace='/dash')

I have tried using both the Eventlet and Gevent pip library but both seem to not help the issue (I uninstalled Eventlet as I have read that it takes preference over Gevent)

The application uses blueprints which encapsulates the socket.py

socket.py

@socketio.on('connect', namespace='/dash')
def test_connect():
    print("Socket connected!")
    global thread
    with thread_lock:
        if thread is None:
            thread = socketio.start_background_task(target=background_thread)
    emit('my_response', {'data': 'Connected', 'count': 0})

app.py

app = create_app()

@app.shell_context_processor
def make_shell_context():
    return {'db': db, 'User': User}

if __name__ == "__main__":
    app.secret_key = os.urandom(12)
    socketio.run(app, host='0.0.0.0', use_reloader=False, debug=True)

client code (socket.io v1.3.5)

socket = io.connect('http://' + document.domain + ':' + location.port + '/dash');
        socket.on('connect', function() {
            socket.emit('join_room', {room_id: room_id});
        });

client network tab of debug
image

server debug
image

What are some possible causes for this behaviour? Thanks in advance.

@miguelgrinberg
Copy link
Owner

The raspberry pi log shows that connections are being received and are responded with status code 200. That means that as far as the Pi is concerned the client is connecting. You may want to add extra logging to see if something appears there. Add engineio_logger=True to your SocketIO constructor or socketio.init_app() call for that.

@Rory-Wade
Copy link
Author

Rory-Wade commented Jul 8, 2018

After a lot more debug I have come to the conclusion that calling app = create_app() was the culprit for the issue. Within the flask application was a MQTT handler using Flask_mqtt and to access SQLAlchemy the functions need an app reference. However, I still don't understand why calling create_app() breaks socketio's ability to call events like "connect" and how I would go about getting an app reference without breaking socketio?

Very similar issue to #651.

app/init.py

...

db = SQLAlchemy()
socketio = SocketIO()
mqtt = Mqtt()
csrf = CSRFProtect()
login = LoginManager()
login.login_view = 'auth.login'

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    csrf.init_app(app)
    db.init_app(app)
    mqtt.init_app(app)

    socketio.init_app(app,async_mode="eventlet", engineio_logger=True)
    login.init_app(app)

...
    from app.iot import bp as iot_bp
    app.register_blueprint(iot_bp, url_prefix='/iot')

    return app

from app import models

app/iot_manager.py

from flask import url_for
from app import db, mqtt, csrf,create_app
from app.models import User, Still, Sensor, Sensor_Data
from threading import Lock
from app.iot import bp
import struct
import logging

print("MQTT START")
app = create_app()

@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
    with app.app_context():
          sensor = Sensor.query.filter_by(guid="random").first()

@miguelgrinberg
Copy link
Owner

This issue will be automatically closed due to being inactive for more than six months. Seeing that I haven't responded to your last comment, it is quite possible that I have dropped the ball on this issue and I apologize about that. If that is the case, do not take the closing of the issue personally as it is an automated process doing it, just reopen it and I'll get back to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants