Files
kennethreitz.org/data/essays/2013-01-introducing_flask_sockets.md
kennethreitz c148fd37bd Self-host all legacy images — zero external dependencies
Downloaded 217 images (131 Squarespace, 85 Flickr, 1 AI section)
to /static/images/legacy/ and updated all references across 60 files.
Removed 1 broken Flickr image. Site now serves all images locally.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 18:58:22 -04:00

1.9 KiB

Introducing Flask-Sockets

January 2013

The state of WebSockets in Python is unfortunate — there's no obvious way to do it. Twisted + Autobhan? Node.js + HAProxy? Diesel.io? Nothing feels right. Let's create a WebSocket echo endpoint.

from flask import Flask
from flask_sockets import Sockets

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

@sockets.route('/echo')
def echo_socket(ws):
    while True:
        message = ws.receive()
        ws.send(message)

@app.route('/')
def hello():
    return 'Hello World!'

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

I'm going to use the shit out of this. — Randall Degges

This looks absolutely incredible. — Glenn Siegman

How do you install this in node? — Nick Hudkins

You are a golden god, sir. — Jeremy Bowers

*foams at the mouth* — Kyle Conroy

Installation & Deployment

Flask-Sockets is an easy to install Flask extension:

$ pip install Flask-Sockets

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.

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

$ gunicorn -k flask_sockets.worker hello:app

Everything else is taken care of for you.

Moving Forward

If you'd like to help bring this library to the next level, fork it and send a pull request!