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
Architecting with Channels
Search
Andrew Godwin
July 19, 2016
Programming
2
1.2k
Architecting with Channels
My keynote from DjangoCon US 2016.
Andrew Godwin
July 19, 2016
Tweet
Share
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
360
Django Through The Years
andrewgodwin
0
270
Writing Maintainable Software At Scale
andrewgodwin
0
490
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
390
Async, Python, and the Future
andrewgodwin
2
710
How To Break Django: With Async
andrewgodwin
1
770
Taking Django's ORM Async
andrewgodwin
0
760
The Long Road To Asynchrony
andrewgodwin
0
720
The Scientist & The Engineer
andrewgodwin
1
810
Other Decks in Programming
See All in Programming
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
6
1.7k
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
170
ThorVG Viewer In VS Code
nors
0
700
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.6k
フロントエンド開発の勘所 -複数事業を経験して見えた判断軸の違い-
heimusu
7
2.6k
インターン生でもAuth0で認証基盤刷新が出来るのか
taku271
0
180
Denoのセキュリティに関する仕組みの紹介 (toranoana.deno #23)
uki00a
0
250
re:Invent 2025 トレンドからみる製品開発への AI Agent 活用
yoskoh
0
680
React 19でつくる「気持ちいいUI」- 楽観的UIのすすめ
himorishige
11
5.5k
Python札幌 LT資料
t3tra
7
1.1k
Claude Codeの「Compacting Conversation」を体感50%減! CLAUDE.md + 8 Skills で挑むコンテキスト管理術
kmurahama
1
780
Kotlin Multiplatform Meetup - Compose Multiplatform 외부 의존성 아키텍처 설계부터 운영까지
wisemuji
0
180
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
247
13k
AI: The stuff that nobody shows you
jnunemaker
PRO
2
190
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
65
36k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
110
Ruling the World: When Life Gets Gamed
codingconduct
0
130
Side Projects
sachag
455
43k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
190
Docker and Python
trallard
47
3.7k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
Building the Perfect Custom Keyboard
takai
2
670
Believing is Seeing
oripsolob
1
33
Transcript
None
Andrew Godwin Hi, I'm Django core developer Senior Software Engineer
at Used to complain about migrations a lot
2010
WebSockets
WebSockets (and Eventlet)
2012
None
2014
"django-onair"
2015
Channels
Born from WebSockets
Born from WebSockets Expanded to be more
The "real hard problem"
Asynchronous coordination
Why?
WebSockets don't magically solve everything
Architecture is about tradeoffs
Bidirectional, low overhead Complex client-side, compatibility CONS PROS
Streaming updates Chat applications Collaborative editing Game backends
Broadcast
Server Client 1 Client 2 Client 3 Client 4
Server Client 1 Client 2 Client 3 Client 4 Server
Server Client 1 Client 2 Client 3 Client 4 Server
The "hard problem"
Channels is a foundation for runnng 'async' at scale
WebSockets Task offloading Chat/Email integration IoT protocols
What makes it hard?
Stateful connections Internal network Bottlenecks
Server Server Django Script ASGI "Send to channel X" "Receive
from channel X" "Send to group Y" "Add channel X to group Y" "Remove channel X from group Y"
Server Server Django Script ASGI ASGI ASGI ASGI Redis
Server Server Django Script ASGI ASGI ASGI ASGI Shared Memory
Server Server Django Script ASGI ASGI ASGI ASGI Shared Memory
Redis Redis
bit.ly/asgi-spec
Channels wraps the low-level ASGI operations
Think of it as the "Django bit" of a larger
whole.
Daphne HTTP/WebSocket Server Channels Django integration asgi-redis Redis backend asgi-ipc
Local memory backend asgiref Shared code and libs
What does Channels provide? Routing Consumers Sessions Auth Helpers By
channel, URL, etc. Standardised message handling Cross-network persistence on sockets Including HTTP cookies on WebSocket runserver, runworker, debugging info
Putting it to use Let's make a chat!
Consumers def on_connect(message): Group("chat").add(message.reply_channel) def on_receive(message): Group("chat").send({"text": message["text"]}) def on_disconnect(message):
Group("chat").discard(message.reply_channel) websocket.connect websocket.receive websocket.disconnect
Routing from channels import route routing = [ route("websocket.connect", on_connect),
route("websocket.receive", on_receive), route("websocket.disconnect", on_disconnect) ]
Class-based from channels import route_class routing = [ route_class(ChatConsumer), ]
from channels.generic.websockets class ChatConsumer(WebsocketConsumer): def connection_groups(self): return ["chat"] def receive(self, text): self.group_send("chat", text=text) Routing
Full worked example github.com/andrewgodwin/channels-examples
Liveblog
Liveblog
Data Binding Multiplexing &
class IntegerValue(models.Model): name = models.CharField(max_length=100, unique=True) value = models.IntegerField(default=0) class
IntegerValueBinding(WebsocketBinding): model = IntegerValue stream = "intval" def check_permission(self, user, action, instance): return True def group_names(self, instance, action): return ["binding.values"]
Demo Do I dare?
More in channels-examples!
Custom Protocols
Decompose into messages Email email.send, email.receive Slack slack.receive, slack.send, slack.joined,
... MQTT mqtt.subscribe, mqtt.unsubscribe, mqtt.send, ...
Write a custom server Use asyncio, Twisted, Gevent, etc Server
takes path to ASGI channel layer instance (CLI/config file) Roll layer.send, layer.receive_many into your event loop
Write Django consumers Same codebase as the main project Optional
routing/per-worker channel limiting
IRC Worker ASGI Worker Worker Email MQTT Scheduler HTTP/WS Custom
Daemon
Channels is a tool for you to use
There's more to be done (and some funding for it)
1995 You are a desktop app 2005 You are a
website 2015 You are a rich web/mobile app 2025 ?
What are the goals of a framework? Do we adapt?
Let's keep Django and Python a great choice for complex
backends
Thanks. Andrew Godwin @andrewgodwin channels.readthedocs.io github.com/andrewgodwin/channels-examples