An Introduction to
Django Channels
Scott Burns
@scottsburns
Slide 2
Slide 2 text
We’re hiring!
Let’s talk after?
Slide 3
Slide 3 text
WSGI
• Web Server Gateway Interface
• Defines the communication between
applications/frameworks and servers.
• PEP 333 — Dec. 2003
• Py3 addressed in PEP 3333
• Request→Response
Challenges
• Python async is improving quickly
• Building a websocket-capable server is doable
with autobahn, etc
• How to actually build applications though?
Slide 13
Slide 13 text
Challenges, cont.
• Once you have servers with active web sockets,
what next?
• Message sockets on different machines?
• Broadcast to many sockets (again, across many
machines)?
• Dead sockets?
• Distributed systems are hard!
Slide 14
Slide 14 text
Real application
?
?
Slide 15
Slide 15 text
Channels
• Adds a messaging layer between Django and
interface servers
• Moves the (difficult, error prone) async code to
the interface, letting application developers
continue to write synchronous code (yay!)
Slide 16
Slide 16 text
Interface Server (Daphne)
ChannelLayer
Worker Worker Worker
Clients
Slide 17
Slide 17 text
Glossary
• Consumers: handler functions for messages
• Analogous to Django views
• Interface servers: async servers to which clients
connect (and stay connected?)
• channels.ChannelLayer: transport layer for moving
messages between interfaces and consumers
• These differ based on the deployment strategy
Slide 18
Slide 18 text
Channel
Named first-in, first-out task queue
Group
Named, broadcast-able sets of channels
Consume on…
• websocket.connect: add the (response) channel
to the chat Group.
• websocket.receive: broadcast the message to
connected sockets in the Group.
• websocket.disconnect: remove the particular
socket from the Group.
Slide 27
Slide 27 text
Demo
Slide 28
Slide 28 text
Deployments
Slide 29
Slide 29 text
Interface Server (Daphne)
ChannelLayer
Worker Worker Worker
Clients
Interface Server (Daphne)
Worker Worker Worker
Machine 1 Machine 2
Slide 30
Slide 30 text
Interface Server (Daphne)
ChannelLayer
Worker Worker Worker
Clients
Interface Server (Daphne)
Worker Worker Worker
Machine 1 Machine 2
ChannelLayer