Flask-SocketIO is a Flask extension that enables real-time bidirectional communication between clients and servers using the Socket.IO protocol. It provides features like event-based communication, rooms for grouping clients, namespaces for organization, and automatic reconnection. The library is actively maintained, with frequent releases, and its current version is 5.6.1.
pip install Flask-SocketIOVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a basic Flask-SocketIO application. It initializes a Flask app, wraps it with `SocketIO`, and defines event handlers for `my event` and `message`. The `my event` handler echoes data back to the sender, while the `message` handler broadcasts to all connected clients. Crucially, `socketio.run(app)` is used instead of `app.run()` to start the server, enabling WebSocket support. For production, asynchronous workers like eventlet or gevent are recommended.
Ensure your client-side Socket.IO library (e.g., JavaScript client) is compatible with Socket.IO protocol v3 or later. Upgrade `socket.io-client` in your frontend project (e.g., `npm install socket.io-client@^4.0.0`).
Replace `app.run()` with `socketio.run(app)`. Additionally, for production, install an asynchronous worker (e.g., `pip install gevent`).
Install either `gevent` (`pip install gevent`) or `eventlet` (`pip install eventlet`). `gevent` is recommended. If using a message queue, 'monkey patching' of the standard library might be required by calling `eventlet.monkey_patch()` or `from gevent import monkey; monkey.patch_all()` at the very top of your main script.
Configure a message queue (e.g., `socketio = SocketIO(app, message_queue='redis://localhost:6379')`) and ensure your load balancer uses 'sticky sessions' (e.g., `ip_hash` in Nginx). Install the corresponding message queue package (e.g., `pip install redis`).
Be aware of this session isolation. If shared session state between HTTP and SocketIO is critical, consider server-side session extensions (e.g., Flask-Session, Flask-KVSession) and initialize `SocketIO(app, manage_session=False)` to allow Flask's session management to be used.
For any potentially blocking operations, offload them to a background task or thread using `socketio.start_background_task()` or a dedicated task queue (e.g., Celery). For example: `socketio.start_background_task(my_long_running_function, arg1, arg2)`.
Ensure that the versions of your client-side Socket.IO library (e.g., from a CDN or npm) and your server-side Python packages (`Flask-SocketIO`, `python-socketio`, `python-engineio`) are compatible. Consult the Flask-SocketIO documentation for recommended compatible versions.
Always use `socketio.run(app)` to start your Flask-SocketIO server. For production environments, ensure you have installed an asynchronous web server like `eventlet` (`pip install eventlet`) or `gevent` (`pip install gevent`).
Verify that your client-side connection URL correctly specifies the Socket.IO namespace (e.g., `io('/my_namespace')`) if your server-side event handlers are defined for a specific namespace. Do not confuse Flask blueprint `url_prefix` with Socket.IO namespaces, as they operate independently.Install the `Flask-SocketIO` package using pip: `pip install Flask-SocketIO`. Also, ensure that the import statement is `from flask_socketio import SocketIO` and not `from Flask_SocketIO import SocketIO` or similar.