Professional Documents
Culture Documents
for the
REAL-TIME WEB
Andrew Godwin
@andrewgodwin
Hi, I'm
Andrew Godwin
Django core developer
Senior Software Engineer at
Likes complaining about networking
Browser
HTTP
Webserver
WSGI
Django
Handler
View
Browser
Server
reque
st
se
respon
reque
st
e
s
respon
reque
st
s
respon
reque
st
s
respon
HTTP 1
Browser
Server
reque
st
se
respon
reque
st
e
s
respon
reque
st 1
reque
st 2
se 1
n
o
p
s
e
r
e2
s
n
o
p
s
re
HTTP 2
Browser
Server
send
send
receiv
e
send
receiv
e
receiv
e
send
WebSockets
???
Easy to use
Secure by default
Hard to break/deadlock
Python 2 & 3 compatible
Optional
Message-passing
Process 1
HTTP
WSGI
Server
WSGI
WSGI
App
Process 2
HTTP
WSGI
Server
WSGI
WSGI
App
Process 1
WebSocket
Worker
Protocol
Server
Worker
Worker
Process 2
WebSocket
Worker
Protocol
Server
Worker
Process 1
WebSocket
Protocol
Server
Worker
Process 2
WebSocket
Protocol
Server
Synchronous
Django project
Process 4
Asynchronous
socket handling
ASGI
Worker
Server
Process 1
Interface
Server
Interface
Server
Process 2
ASGI
ASGI
Channel
Layer
ASGI
ASGI
Worker
Server
Worker
Server
Process 3
Worker 1
Worker 2
Worker 3
Interface
Server
Interface
Server
Interface
Server
Channel Layer
Channel Layer
Channel Layer
Worker
Server
Worker
Server
Worker
Server
Channel Layer
Redis
Channels
Named FIFO task queues
Groups
Named sets of channels you broadcast to
group_add("group_name", "channel_name")
group_discard("group_name", "channel_name")
send_group("group_name", {"ponies": True})
Request
View
Response
Message
Consumer
Messages
websocket.connect
Consumer
websocket.receive
Consumer
websocket.send
websocket.connect
Consumer
websocket.receive
Consumer
websocket.send!jq3x41
http.request
Consumer
http.response!a34cxw
Worker
websocket.receive
Worker
Worker
websocket.send!a1b2c3
Socket
Using it
Installation
Liveblog
Liveblog
People open a WebSocket when they open the page
Their WebSocket is added to a group
When the BlogPost model is saved, we send the
post to that group
Chat
Chat
Important notes
Runserver just works with WebSockets now
Django sessions + auth work with WebSockets
Generic Consumers exist
Fully worked versions of these two are at
github.com/andrewgodwin/channels-examples
Channel
Group
Messages
ASGI
&
Redis
POSIX IPC
In-memory
For testing or single-process installs
Synchronous
Django project
Process 4
Asynchronous
socket handling
ASGI
Worker
Server
Process 1
Interface
Server
Interface
Server
Process 2
ASGI
ASGI
Channel
Layer
ASGI
ASGI
Worker
Server
Worker
Server
Process 3
Interface
Server
Channel
Layer
Worker
Server
Daphne
asgi_redis
Django
HTTP + WS
Cross-network, shards
Consumer system
WSGI Adapter
asgi_ipc
WSGI Adapter
HTTP
Single-machine
Worker 1
Worker 2
Interface
Server
Interface
Server
Channel Layer
Channel Layer
Worker
Server
Worker
Server
Channel Layer
Redis
Worker 3
WSGI
Server
WSGI
App
Scaling?
Looking Ahead
Being Django
Ofcial external app
Merge in 1.11 or 2.0
Maturity
Load tests and tweaking
Learning from production installs
Community
Third-party tools and extensions
Tutorials, write-ups and case studies
Co-existence
Not everyone needs WebSockets!
Make sure it's easy to add or remove.
Expansion
Email, chat and other message formats
Add more events to code against!
Specication
ASGI is not Django-specic
WSGI needs supplementing for new protocols
channels.readthedocs.io
github.com/andrewgodwin/channels-examples
Thanks.
Andrew Godwin
@andrewgodwin