Change the future

Friday 5:10 p.m.–5:40 p.m.

Make More Responsive Web Applications with SocketIO and GEvent

Luke Sneeringer

Audience level:
Web Frameworks


An explanation of how to implement a server in Python to serve websocket requests from browsers.



Sometimes, it seems the web is always behind. Often, that's because it is. Because every new feature has to work in every browser before anyone can use it professtionally, progress on the web is often glacial.

That said, the new JavaScript library elegantly solves much of the "last mile" problem -- getting information to browsers without the browser having to repeatedly ask for it. And, there's a Python-based server that runs on top of the Python async library gevent, making Python an excellent choice for server-side code powering these applications.

This talk will run through how to implement a gevent server (using Django, but that's incidental; any Python framework can work). I will assume no knowledge of gevent or, but do presume intermediate-to-experienced Python knowledge.


  • Introduction to the Topics (3 minutes)
    • Breaking News: traditional HTTP is stateless and client-driven
    • The server can't just decide to send things to a client.
    • We've been working around this restriction for a decade.
    • is the newest, browser-compatible way to maintain a stateful connection between browser and server
  • What looks like on the client side (5 minutes)
    • Explanation of how connect works
    • It has multiple backends (websocket, flash, ajax...), and picks the "best" one based on browser compatibility.
    • What it is doing on the backend doesn't really matter; that can be a black box to the web developer -- therein lies the beauty.
    • Explanation of events
    • Really just listeners: "I want to know when X happens."
    • The server can (and usually does) broadcast more events than the client requests.
  • Implementing this on the server side (15 minutes)
    • A quick overview of what's available.
    • You can use JavaScript on the server side too; this was designed for node.js. But, this is _Py_Con.
    • There are advantages to using gevent; a big one: greenlets are much easier to debug
    • Let's set up a gevent server!
    • There's not that much involved, actually. The Socket IO server can take the same WSGI application object that Django does. Therefore, Django has already done most of the work for us.
    • Display of a SocketIO server that operates as a "regular" Django server -- it does the boring stuff.
    • But the boring stuff isn't enough: we want it to handle websocket requests!
    • Setting up the URL and the Django view
    • Namespaces: Wait, what are these?
      • they determine what group of views the client wants when it connects to that namespace
      • and this is where most of our code goes
      • this code can talk to Django normally...mostly (but request is dangerous!)
    • Let's look at what our namespace code might look like to give clients notifications when interesting things happen.
      • dependency: Redis
      • at its most basic, it's two steps:
      • subscribe
      • listen
        • a simple Python listener can call emit as needed when Redis broadcasts
        • and it "just works" -- display something going in and immediately being shown
  • Q&A (5 minutes)