Overview
Flask-SocketIO 5.0, released on March 15, 2021, upgrades to the Socket.IO 5.x protocol and adds native async mode.
Main Features
Socket.IO 5.x protocol
Flask-SocketIO 5.0 adopts the Socket.IO 5 protocol which improves connection reliability, binary support, and reconnection handling.
python
from flask import Flask
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins='*')
@socketio.on('message')
def handle_message(data):
print(f'Received: {data}')
emit('response', {'reply': 'Message received!'})
@socketio.on('connect')
def handle_connect():
print('Client connected')
emit('welcome', {'message': 'Connection established'})
# socketio.run(app, host='0.0.0.0', port=5000)
Async mode
Native async mode allows using async/await in event handlers, improving performance for concurrent connections.
python
from flask import Flask
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app, async_mode='eventlet')
@socketio.on('process')
def long_process(data):
"""Processing with progressive emission."""
import time
for i in range(5):
time.sleep(1)
emit('progress', {'step': i + 1, 'total': 5})
emit('done', {'result': 'OK'})
# Supported async modes: eventlet, gevent, threading
