Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Elegant WebSockets for your Flask apps.
branch: master

Merge pull request #20 from jhorneman/master

Making sure all paths return something
latest commit 2a02568e07
@kennethreitz authored



Elegant WebSockets for your Flask apps.
from flask import Flask
from flask_sockets import Sockets

app = Flask(__name__)
sockets = Sockets(app)

def echo_socket(ws):
    while True:
        message = ws.receive()

def hello():
    return 'Hello World!'

Serving WebSockets in Python was really difficult. Now it's not.


To install Flask-Sockets, simply:

$ pip install Flask-Sockets


A custom Gunicorn worker is included to make deployment as friendly as possible:

$ gunicorn -k flask_sockets.worker hello:app

Production services are provided by gevent and gevent-websocket.

Anything that inserts wsgi.websocket into the WSGI environ is supported, but gevent-websocket is recommended.

Development / Testing

Because the Werkzeug development server cannot provide the WSGI environ with a websocket interface, it is not possible to run a Flask app using the standard

If you try to, Flask will still try to serve on all the specified routes, and throw a KeyError whenever a client tries to connect to a websocket route.

Instead, just use the included gunicorn worker (explained above), or anything that can insert wsgi.websocket into the WSGI environ.

WebSocket Interface

The websocket interface that is passed into your routes is provided by gevent-websocket. The basic methods are fairly straitforward — send, receive, send_frame, and close.

Hopefully, more documentation will be available soon.

Something went wrong with that request. Please try again.