python-3.xflasksocket.ioflask-socketio

Issues Connecting Flask app with Flask-Socket.io


I have a flask app and I am trying to get flask-socket.io to work. The app name is rocket_launcher.

rocket_launcher/init.py has

from flask_socketio import SocketIO

def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=False)
    # other app creation stuff

#socketio = SocketIO(create_app(),logger=True, engineio_logger=True)   
socketio = SocketIO(logger=True, engineio_logger=True)   

@socketio.on('message')
def handle_message(data):
    logger.debug('received message: ' + data)

if __name__ == '__main__':
    app = create_app(host='0.0.0.0', port=5000, debug=True)
    socketio.run(app, host='0.0.0.0', port=5000, debug=True)

My shell script (run.sh) to run my app is:

#!/bin/bash
export FLASK_APP=rocket_launcher
export FLASK_DEBUG=True
#python -m flask run --host=0.0.0.0
flask --app rocket_launcher run --host=0.0.0.0

The javascript I load with the starting page:

var socket = io()
socket.on('connect', function() {
    socket.emit('my_event', {data:"I\'m connected!"});
});

When the app starts, it works (except the socketio stuff). For output, I get:

(rocket_launcher_flask) mark@piranha:~/python-projects/rocket_launcher_flask$ ./run.sh 
sys.path=['/home/mark/.virtualenvs/rocket_launcher_flask/bin/python', '/home/mark/python-projects/rocket_launcher_flask/rocket_launcher', '/home/mark/python-projects/rocket_launcher_flask', '/home/mark/.virtualenvs/rocket_launcher_flask/bin', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/mark/.virtualenvs/rocket_launcher_flask/lib/python3.10/site-packages']
2023-09-07:22:44:47,636 DEBUG    [expansion_board_1.py:25] LHF init
2023-09-07:22:44:47,636 DEBUG    [expansion_board_1.py:30] Launcher_Hardware_Factory create_object
2023-09-07:22:44:47,637 DEBUG    [expansion_board_1.py:51] No hardware
No hardware
2023-09-07:22:44:47,637 INFO     [fsm.py:19] fsm __init__
started
2023-09-07:22:44:47,640 DEBUG    [model.py:16] started
 * Serving Flask app 'rocket_launcher'
 * Debug mode: on
2023-09-07:22:44:47,642 INFO     [_internal.py:187] WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://192.168.50.132:5000
2023-09-07:22:44:47,643 INFO     [_internal.py:187] Press CTRL+C to quit
2023-09-07:22:44:47,643 INFO     [_internal.py:187]  * Restarting with stat
sys.path=['/home/mark/.virtualenvs/rocket_launcher_flask/bin/python', '/home/mark/python-projects/rocket_launcher_flask/rocket_launcher', '/home/mark/python-projects/rocket_launcher_flask', '/home/mark/.virtualenvs/rocket_launcher_flask/bin', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/mark/.virtualenvs/rocket_launcher_flask/lib/python3.10/site-packages']
2023-09-07:22:44:48,024 DEBUG    [expansion_board_1.py:25] LHF init
2023-09-07:22:44:48,024 DEBUG    [expansion_board_1.py:30] Launcher_Hardware_Factory create_object
2023-09-07:22:44:48,025 DEBUG    [expansion_board_1.py:51] No hardware
No hardware
2023-09-07:22:44:48,025 INFO     [fsm.py:19] fsm __init__
started
2023-09-07:22:44:48,028 DEBUG    [model.py:16] started
2023-09-07:22:44:48,030 WARNING  [_internal.py:187]  * Debugger is active!
2023-09-07:22:44:48,030 INFO     [_internal.py:187]  * Debugger PIN: 751-117-059
2023-09-07:22:44:49,948 INFO     [_internal.py:187] 192.168.50.132 - - [07/Sep/2023 22:44:49] "GET /socket.io/?EIO=4&transport=polling&t=OfpKbGI HTTP/1.1" 404 -
2023-09-07:22:44:55,953 INFO     [_internal.py:187] 192.168.50.132 - - [07/Sep/2023 22:44:55] "GET /socket.io/?EIO=4&transport=polling&t=OfpKckA HTTP/1.1" 404 -
2023-09-07:22:45:01,968 INFO     [_internal.py:187] 192.168.50.132 - - [07/Sep/2023 22:45:01] "GET /socket.io/?EIO=4&transport=polling&t=OfpKeC9 HTTP/1.1" 404 -
2023-09-07:22:45:07,982 INFO     [_internal.py:187] 192.168.50.132 - - [07/Sep/2023 22:45:07] "GET /socket.io/?EIO=4&transport=polling&t=OfpKfg7 HTTP/1.1" 404 -
2023-09-07:22:45:14,087 INFO     [_internal.py:187] 192.168.50.132 - - [07/Sep/2023 22:45:14] "GET /socket.io/?EIO=4&transport=polling&t=OfpKh9X HTTP/1.1" 404 -
2023-09-07:22:45:20,101 INFO     [_internal.py:187] 192.168.50.132 - - [07/Sep/2023 22:45:20] "GET /socket.io/?EIO=4&transport=polling&t=OfpKidU HTTP/1.1" 404 -

I also do not get any socket.io logging or error messages. What am I doing wrong?

Thanks!


Solution

  • I suggest you work with the Flask-SocketIO documentation and the provided examples, as there are a couple of things you are missing.

    First of all, the SocketIO() class needs to be initialized with your app instance. You should move it inside your create_app() function:

    from flask_socketio import SocketIO
    
    socketio = SocketIO()
    
    def create_app(test_config=None):
        # create and configure the app
        app = Flask(__name__, instance_relative_config=False)
        socketio.init_app(app, logger=True, engineio_logger=True)   
    
        # other app creation stuff
        return app
    

    Next, you have to use the socketio.run() method to run the server, not flask run. One way to do this is to remove the if __name__ == '__main__' block from rocket_launcher/__init__.py and put it in a top-level run.py file, for example like this:

    from rocket_launcher import create_app, socketio
    
    if __name__ == '__main__':
        app = create_app()
        socketio.run(app, host='0.0.0.0', port=5000, debug=True)
    

    To start the server, then you start run.py as a regular script:

    python run.py