Slide 1

Slide 1 text

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

Slide 4

Slide 4 text

Django • “Batteries Included” Framework • Object-Relational Mapper • Templating • URL Routing • Signals, etc • Large 3rd-party ecosystem https://www.djangoproject.com/community/logos/

Slide 5

Slide 5 text

Quick Demo

Slide 6

Slide 6 text

Client (Browser, cURL, Native, etc) HTTP WSGI time Application Server

Slide 7

Slide 7 text

WSGI Server (Gunicorn, uWSGI, etc) WSGI App Really… WSGI App WSGI App

Slide 8

Slide 8 text

Client time Application HTTP 2

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Client time Application WebSockets …

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

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

Slide 19

Slide 19 text

send(“channel_name”, {“type”: “message”}) receive_many([“channel_one”, “channel_two”]) group_add(“group”, “channel_name”) group_discard(“group”, “channel_name”) send_group(“group”, {“type”: “message”})

Slide 20

Slide 20 text

HTTP View Request Response

Slide 21

Slide 21 text

Messages Consumer Message Message Message

Slide 22

Slide 22 text

$ pip install channels INSTALLED_APPS = [ …, channels, …, ]

Slide 23

Slide 23 text

Let’s build a chat

Slide 24

Slide 24 text

One room, all clients receive all messages

Slide 25

Slide 25 text

One view to serve the page & assets

Slide 26

Slide 26 text

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

Slide 31

Slide 31 text

Interface ChannelLayer Wor Wor Wor ChannelLayer Interface ChannelLayer Wor Wor Wor Interface ChannelLayer Wor Wor Wor (RedisLocalChannelLayer)

Slide 32

Slide 32 text

Philosophically • It’s not just about WebSockets • Generalized eventing for Django • Many Protocols • Multi-framework backends?

Slide 33

Slide 33 text

Links • Andrew Godwin’s 2016 PyCon Talk • Channels docs (channels.readthedocs.org) are fabulous • http://www.aeracode.org/2016/6/16/philosophy- channels/

Slide 34

Slide 34 text

Questions?