Flask: A RESTful API and SocketIO Server Flask: A RESTful API and SocketIO Server flask flask

Flask: A RESTful API and SocketIO Server


You just want to run socketio.run(app, port=5005) and hit the REST API on port 5005.

The reason this works is because under the hood, Flask-SocketIO is running an evented webserver based on gevent (or with the 1.0 release, also eventlet) - this server handling the websocket requests directly (using the handlers you register via the socketio.on decorator) and is passing on the non-websocket requests to Flask.

The reason your code wasn't working is because both app.run and socketio.run are blocking operations. Whichever one ran first was looping, waiting for connections, never allowing the second to kick off. If you really needed to run your websocket connections on a different port you'd need to spawn either the socketio or the app run call on a different process.