{"id":4013,"library":"flask-sock","title":"Flask-Sock","description":"Flask-Sock provides WebSocket support for Flask applications, enabling real-time, bi-directional communication. Unlike other solutions, it works directly with the Flask development web server and does not require a greenlet-based server like gevent or eventlet for basic operation. It is also compatible with production WSGI servers such as Gunicorn (with threading), Eventlet, or Gevent. The current version is 0.7.0, with a periodic release cadence.","status":"active","version":"0.7.0","language":"en","source_language":"en","source_url":"https://github.com/miguelgrinberg/flask-sock","tags":["flask","websocket","realtime","web"],"install":[{"cmd":"pip install flask-sock","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core web framework dependency.","package":"Flask","optional":false},{"reason":"Provides the underlying WebSocket implementation.","package":"simple-websocket","optional":false},{"reason":"Required Python version.","package":"python","optional":false}],"imports":[{"symbol":"Sock","correct":"from flask_sock import Sock"},{"symbol":"Flask","correct":"from flask import Flask"}],"quickstart":{"code":"from flask import Flask\nfrom flask_sock import Sock\nimport os\n\napp = Flask(__name__)\nsock = Sock(app)\n\n@app.route('/')\ndef index():\n    return 'Hello, Flask-Sock!'\n\n@sock.route('/echo')\ndef echo(ws):\n    while True:\n        data = ws.receive()\n        if data is None: # Client disconnected\n            break\n        print(f\"Received: {data}\")\n        ws.send(data)\n\nif __name__ == '__main__':\n    # For development, Flask's built-in server works. \n    # For production, use Gunicorn with --threads, Eventlet, or Gevent.\n    app.run(debug=True)","lang":"python","description":"This quickstart sets up a basic Flask application with a WebSocket endpoint that echoes back any received message. Instantiate `Flask` and `Sock`, then use the `@sock.route()` decorator to define WebSocket handlers. The `ws` object within the handler provides `receive()` and `send()` methods for communication."},"warnings":[{"fix":"Upgrade your Flask installation to version 2.0.0 or higher (`pip install 'Flask>=2.0.0'`).","message":"Version 0.4.0 of `flask-sock` and newer explicitly introduced a dependency on `Flask 2.x`. Applications using these versions must ensure they are running Flask 2.0.0 or higher, as older Flask versions are no longer supported.","severity":"breaking","affected_versions":">=0.4.0"},{"fix":"Use the `--threads` option with Gunicorn to allow a single worker to handle multiple connections concurrently.","message":"When deploying `flask-sock` applications with Gunicorn, each active WebSocket connection consumes a worker process by default. To efficiently handle multiple concurrent WebSocket clients, Gunicorn must be configured to use threads (e.g., `gunicorn -b :5000 --threads 100 module:app`).","severity":"gotcha","affected_versions":"All"},{"fix":"Configure `app.config['SOCK_SERVER_OPTIONS'] = {'ping_interval': 25}` in your Flask application to maintain active connections and prevent timeouts for idle clients.","message":"As of version 0.5.2, WebSocket server options, such as `ping_interval` and `max_message_size`, can be configured via `app.config['SOCK_SERVER_OPTIONS']`. Failing to set `ping_interval` (e.g., to 25 seconds) can lead to unexpected disconnections for idle WebSocket connections due to proxy timeouts.","severity":"gotcha","affected_versions":">=0.5.2"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}