Importing @ socketio.on () from another class in Flask

I am making a new app with Flask and socketio. The simplified code looks like this:

socketio = SocketIO(app)

@socketio.on('connect', namespace='/test')
def test_connect():
    print('Client connected')
    emit('my response', {'data': 'User Connected'}, broadcast = True)


if __name__ == '__main__':
    socketio.run(app, '0.0.0.0', 8080)

      

I'm not sure how I should import the: @ socketio.on ('connect', namespace = '/ test') " handler from another class, so I am not overloading the main class.

I think Blueprints might be the solution, but I just found how to use them with "routes". I'm not sure if this will work with sockets.

Can anyone give me a hand? Thanks everyone

+3


source to share


1 answer


How you can do this, move the functions socketio.on()

to another module. You mentioned classes, but they are really functions, the way to structure your application is to divide its parts into modules or packages.



Take a look at this example I created to demonstrate one way to structure a Flask application using Flask-SocketIO.

+1


source







All Articles