{"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":"python","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,"search_vec":"'0.7.0':69 'also':51 'applic':12 'base':40 'basic':47 'bi':18 'bi-direct':17 'cadenc':74 'communic':20 'compat':52 'current':66 'develop':30 'direct':19,26 'enabl':13 'eventlet':45,62 'flask':2,5,11,29,75 'flask-sock':1,4 'gevent':43,64 'greenlet':39 'greenlet-bas':38 'gunicorn':59 'like':42 'oper':48 'period':72 'product':54 'provid':7 'real':15 'real-tim':14 'realtim':77 'releas':73 'requir':36 'server':32,41,56 'sock':3,6 'solut':23 'support':9 'thread':61 'time':16 'unlik':21 'version':67 'web':31,78 'websocket':8,76 'work':25 'wsgi':55","created_at":"2026-04-12T03:37:16.854959+00:00","updated_at":"2026-04-16T15:08:10.701151+00:00","problems":[{"fix":"Run `pip install flask-sock` to install the library.","cause":"The 'flask_sock' package is not installed in your Python environment or the environment where your application is running.","error":"ModuleNotFoundError: No module named 'flask_sock'"},{"fix":"Ensure your Flask application is running with a compatible WSGI server (like Gunicorn with threads, Eventlet, or Gevent for production) and that your client's WebSocket URL is correct. For development, ensure Flask and Werkzeug are updated to versions 2.0+ which offer better WebSocket support.","cause":"This generic error often occurs when the WebSocket client fails to establish a connection with the server, possibly due to the Flask development server's limitations with WebSockets, incorrect server configuration, or a client-side issue like a misconfigured WebSocket URL.","error":"ConnectionError"},{"fix":"Instead of `sock.register_blueprint()`, define `sock.route` decorators directly on the `Sock` object, or pass the blueprint instance to the `sock.route` decorator's `bp` argument.","cause":"Flask-Sock does not directly support registering WebSocket routes on Flask Blueprints in the same manner as `flask-sockets` or standard Flask HTTP routes.","error":"AttributeError: 'Sock' object has no attribute 'register_blueprint'"},{"fix":"Implement proper error handling (e.g., try-except blocks) around `ws.receive()` and `ws.send()` calls to gracefully handle disconnections. Ensure any stored WebSocket objects are removed from active lists when a client disconnects.","cause":"This error occurs when your application attempts to read from or write to a WebSocket connection after the client has already disconnected or the socket has been explicitly closed.","error":"Receive error -- socket is closed"},{"fix":"Check server-side logs for more specific error messages. If it's a CORS issue, configure `cors_allowed_origins` (if using Flask-SocketIO) or ensure your client's origin is permitted. Verify the client is using the correct WebSocket URL and protocol (e.g., `ws://` or `wss://`).","cause":"The client attempted to establish a WebSocket connection, but the server rejected it with an HTTP 400 Bad Request status, often due to cross-origin issues (CORS), an incorrect WebSocket protocol, or an improperly configured server.","error":"Error during WebSocket handshake: Unexpected response code: 400"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.7.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/miguelgrinberg/flask-sock","docs":null,"changelog":null,"pypi":"https://pypi.org/project/flask-sock/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}