5:10 p.m.–5:40 p.m.
Make More Responsive Web Applications with SocketIO and GEvent
- Audience level:
- Web Frameworks
An explanation of how to implement a socket.io 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.
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 socket.io, 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.
- socket.io is the newest, browser-compatible way to maintain a stateful connection between browser and server
- What socket.io looks like on the client side (5 minutes)
- Explanation of how
- 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.
- 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:
- 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)