Fix WebSocketError: RuntimeError: You need to use a gevent or eventlet server for WebSocket support in Flask
This error occurs when Flask-SocketIO is used with a WSGI server that does not support WebSockets. The default Flask development server and Gunicorn without async workers cannot handle WebSocket connections. Fix it by running your app with eventlet or gevent, or by configuring Gunicorn with the eventlet worker class.
Reading the Stack Trace
Here's what each line means:
- File "/app/venv/lib/python3.12/site-packages/flask_socketio/__init__.py", line 210, in run: Flask-SocketIO's run method initializes the server but detects that neither eventlet nor gevent is installed.
- File "/app/venv/lib/python3.12/site-packages/flask_socketio/__init__.py", line 142, in _init_app: During app initialization, Flask-SocketIO checks for an async-capable server and raises if none is found.
- RuntimeError: You need to use a gevent or eventlet server for WebSocket support.: The error confirms that the current runtime does not support WebSocket connections.
Common Causes
1. Eventlet or gevent not installed
Flask-SocketIO requires an async networking library but neither eventlet nor gevent is listed in requirements.
# requirements.txt
Flask==3.0.0
Flask-SocketIO==5.3.6
# missing: eventlet or gevent
2. Wrong Gunicorn worker class
Gunicorn is started with the default sync worker, which does not support WebSocket connections.
# Procfile
web: gunicorn app:app # default sync worker, no WebSocket support
3. Using flask run instead of socketio.run
The development server is started with flask run which does not initialize the SocketIO server.
# Terminal:
$ flask run # does not start the SocketIO server
The Fix
Install eventlet and configure Gunicorn to use the eventlet worker class. This gives the server async capabilities needed for WebSocket connections. Use a single worker (-w 1) with eventlet since it handles concurrency via green threads.
# Procfile
web: gunicorn app:app
# requirements.txt (add eventlet)
eventlet==0.35.2
# Procfile
web: gunicorn --worker-class eventlet -w 1 app:app
# Or for development:
# socketio.run(app, debug=True)
Testing the Fix
import pytest
from app import create_app, socketio
@pytest.fixture
def client():
app = create_app()
app.config['TESTING'] = True
return socketio.test_client(app)
def test_websocket_connects(client):
assert client.is_connected()
def test_websocket_receives_event(client):
client.emit('ping')
received = client.get_received()
assert len(received) > 0
assert received[0]['name'] == 'pong'
def test_websocket_disconnects_cleanly(client):
client.disconnect()
assert not client.is_connected()
Run your tests:
pytest tests/ -v
Pushing Through CI/CD
git checkout -b fix/flask-websocket-eventlet,git add requirements.txt Procfile,git commit -m "fix: add eventlet worker for WebSocket support",git push origin fix/flask-websocket-eventlet
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt
- run: pytest tests/ -v --tb=short
- run: flake8 app/
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
pip install bugstack
Step 2: Initialize
import bugstack
bugstack.init(api_key=os.environ["BUGSTACK_API_KEY"])
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Run pytest locally to verify WebSocket connections work with eventlet.
- Open a pull request with the requirements and Procfile changes.
- Wait for CI checks to pass on the PR.
- Have a teammate review and approve the PR.
- Merge to main and verify WebSocket connections work in staging.
Frequently Asked Questions
BugStack verifies WebSocket connections can be established and events flow correctly, then runs your full test suite before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Both work. Eventlet is more commonly used with Flask-SocketIO and has better documentation for this use case. Gevent is slightly faster for some workloads.
With eventlet, use -w 1 because eventlet handles concurrency via green threads. For multiple workers, use sticky sessions with a message queue like Redis.