Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Andrew Godwin Hi, I'm Django core developer Senior Software Engineer at Used to complain about migrations a lot

Slide 3

Slide 3 text

Channels

Slide 4

Slide 4 text

Born from WebSockets

Slide 5

Slide 5 text

Born from WebSockets Expanded to be more

Slide 6

Slide 6 text

The "real hard problem"

Slide 7

Slide 7 text

Asynchronous coordination

Slide 8

Slide 8 text

You take a request... ...and return a response.

Slide 9

Slide 9 text

HTTP 1 request response Browser Server request response request response request response

Slide 10

Slide 10 text

HTTP 2 request response Browser Server request response request 1 response 2 request 2 response 1

Slide 11

Slide 11 text

WebSockets receive send Browser Server send receive send send receive

Slide 12

Slide 12 text

???

Slide 13

Slide 13 text

Server Client 1 Client 2 Client 3 Client 4

Slide 14

Slide 14 text

Server Client 1 Client 2 Client 3 Client 4 Server

Slide 15

Slide 15 text

Server Client 1 Client 2 Client 3 Client 4 Server The "hard problem"

Slide 16

Slide 16 text

Broadcast

Slide 17

Slide 17 text

We need to coordinate between servers

Slide 18

Slide 18 text

Channels is a foundation for runnng 'async' at scale

Slide 19

Slide 19 text

Architecture is about tradeoffs

Slide 20

Slide 20 text

At most once / At least once Ordered / Unordered FIFO / FILO Expiry / Persistence

Slide 21

Slide 21 text

WebSockets Service-oriented Architecture Chat/Email integration IoT protocols

Slide 22

Slide 22 text

What makes it hard?

Slide 23

Slide 23 text

DISTRIBUTED SYSTEMS

Slide 24

Slide 24 text

Stateful connections Internal network Bottlenecks Machines dying

Slide 25

Slide 25 text

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"

Slide 26

Slide 26 text

Server Server Django Script ASGI ASGI ASGI ASGI Redis

Slide 27

Slide 27 text

Server Server Django Script ASGI ASGI ASGI ASGI Shared Memory

Slide 28

Slide 28 text

Server Server Django Script ASGI ASGI ASGI ASGI Shared Memory Redis Redis

Slide 29

Slide 29 text

bit.ly/asgi-spec

Slide 30

Slide 30 text

Channels wraps the low-level ASGI operations

Slide 31

Slide 31 text

Think of it as the "Django bit" of a larger whole.

Slide 32

Slide 32 text

Daphne HTTP/WebSocket Server Channels Django integration asgi-redis Redis backend asgi-ipc Local memory backend asgiref Shared code and libs

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Putting it to use Let's make a chat!

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Routing from channels import route routing = [ route("websocket.connect", on_connect), route("websocket.receive", on_receive), route("websocket.disconnect", on_disconnect) ]

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Full worked example github.com/andrewgodwin/channels-examples

Slide 39

Slide 39 text

Ignoring Django

Slide 40

Slide 40 text

1. Take a channel layer daphne myproject.asgi:channel_layer

Slide 41

Slide 41 text

2. Tie it into an event loop Twisted, asyncio, or while-True

Slide 42

Slide 42 text

3. Call send/receive It's a communication channel!

Slide 43

Slide 43 text

Example: SOA Services receive()-block waiting for tasks Clients use send() with a reply-channel to call an endpoint Servers process and send() the reply

Slide 44

Slide 44 text

IRC Worker ASGI Worker Worker Email MQTT Scheduler HTTP/WS Custom Daemon

Slide 45

Slide 45 text

Channels is a tool for you to use

Slide 46

Slide 46 text

There's more to be done (and some funding for it)

Slide 47

Slide 47 text

The tradeoffs may not be for you! (Especially as you specialise)

Slide 48

Slide 48 text

1995 You are a desktop app 2005 You are a website 2015 You are a rich web/mobile app 2025 ?

Slide 49

Slide 49 text

What are the goals of a framework? Do we adapt?

Slide 50

Slide 50 text

Thanks. Andrew Godwin @andrewgodwin channels.readthedocs.io github.com/andrewgodwin/channels-examples