Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Channels (Under The Hood)
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Andrew Godwin
November 03, 2016
Programming
7.6k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Channels (Under The Hood)
A talk I gave at Django Under The Hood 2016.
Andrew Godwin
November 03, 2016
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
390
Django Through The Years
andrewgodwin
0
320
Writing Maintainable Software At Scale
andrewgodwin
0
520
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
420
Async, Python, and the Future
andrewgodwin
2
740
How To Break Django: With Async
andrewgodwin
1
820
Taking Django's ORM Async
andrewgodwin
0
850
The Long Road To Asynchrony
andrewgodwin
0
770
The Scientist & The Engineer
andrewgodwin
1
850
Other Decks in Programming
See All in Programming
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
130
AIを活用したE2Eテスト実装効率化のあゆみ / ebisu-mobile-14-kotetu
kotetuco
0
140
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
610
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
220
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
170
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
130
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
310
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
420
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
180
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
280
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
280
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
240
Embracing the Ebb and Flow
colly
88
5.1k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
440
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
67
55k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
870
How to Talk to Developers About Accessibility
jct
2
270
Chasing Engaging Ingredients in Design
codingconduct
0
230
Un-Boring Meetings
codingconduct
0
330
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
170
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Transcript
None
Andrew Godwin Hi, I'm Django core developer Senior Software Engineer
at Used to complain about migrations a lot
It's magic.
It's magic.
The Problem 1
The Web is changing.
WebSockets
WebSockets WebRTC Long-polling MQTT Server-Sent Events
Python is synchronous. Django is synchronous.
Synchronous code is easier to write. Single-process async is not
enough.
Proven design pattern Not too hard to reason about
What could fit these constraints?
Loose Coupling 2
Not too tied to WebSockets Not too tied to Django
Well-defined, minimal interfaces
Easy to swap out or rewrite
The Message Bus HTTP Server Message Bus WSock Server Django
Project
What do you send? How do you send it?
ASGI
nonblocking send blocking receive add to group discard from group
send to group
JSON-compatible, dictionary-based messages onto named channels
Concrete Ideas 3
Develop using concrete examples
WebSocket connect receive disconnect accept/reject send
WebSocket websocket.connect websocket.send!abc1234 websocket.receive websocket.disconnect
At-most-once First In First Out Backpressure via capacity Not sticky
No guaranteed ordering No serial processing
HTTP & WS Channel Layer Django Worker HTTP & WS
Django Worker Django Worker Django Worker
{ "text": "Hello, world!", "path": "/chat/socket/", "reply_channel": "websocket.send!9m12in2p", }
Developed and spec'd HTTP WebSocket Rough drafts IRC Email Slack
Please, no. Minecraft Mainframe Terminal
{ "reply_channel": "http.response!g23vD2x5", "method": "GET", "http_version": "2", "path": "/chat/socket/", "query_string":
"foo=bar", "headers": [["cookie", "abcdef..."]], }
At-most-once First In First Out Backpressure via capacity Not sticky
No guaranteed ordering No serial processing
At-most-once First In First Out Backpressure via capacity Not sticky
No guaranteed ordering No serial processing
"order" key on receive messages Connection acceptance
Daphne HTTP/WebSocket Server Channels Django integration asgi-redis Redis backend asgi-ipc
Local memory backend asgiref Shared code and libs
Django-ish 4
It can take several tries to get a nice API.
Consumers based on Views Callable that takes an object Decorators
for functionality Class-based generics
@channel_session def chat_receive(message): name = message.channel_session["name"] message.reply_channel.send({"text": "OK"}) Group("chat").send({ "text":
"%s: %s" % (name, message["text"]), }) Message.objects.create( name=name, content=message["text"], )
Routing based on URLs List of regex-based matches Includes with
prefix stripping on paths More standardised interface
routing = [ route( "websocket.receive", consumers.chat_receive, path=r"^/chat/socket/$", ), include("stats.routing", path="^/stats/"),
route_class(ConsumerClass, path="^/v1/"), ]
Sessions are the only state Sessions hang off reply channels
not cookies Uses same sessions backends Available on the consumer's argument Can also access long-term cookie sessions
@enforce_ordering def receive_consumer(message): Log.objects.create(...)
session = session_for_reply_channel( message.reply_channel.name ) if not session.exists(session.session_key): try: session.save(must_create=True)
except CreateError: # Session wasn't unique raise ConsumeLater() message.channel_session = session
No Middleware New-style middleware half works No ability to capture
sends Decorators replace most cases
View/HTTP Django still there Can intermingle or just use one
type View system is just a consumer now
def view_consumer(message): replies = AsgiHandler()(message) for reply in replies: while
True: try: message.reply_channel.send(reply) except ChannelFull: time.sleep(0.05) else: break
Signals and commands runserver works as expected Signals for handling
lifecycle staticfiles configured for development
Beyond 5
Generalised async communication
Service messaging Security/CPU separation Sync & Async / Py2 &
Py3
Diversity of implementations More web servers More channel layers
More optimisation More efficient bulk sends Less network traffic on
receive
More maintainers More viewpoints, more time
1.0 coming soon Stable APIs for everything except binding
Thanks. Andrew Godwin @andrewgodwin channels.readthedocs.io github.com/andrewgodwin/channels-examples