Upgrade to Pro — share decks privately, control downloads, hide ads and more …

An Introduction to Channels

An Introduction to Channels

I gave this presentation at a recent PyNash evening meeting. There's not much new material beyond Andrew Godwin's PyCon 2016 talk (https://www.youtube.com/watch?v=2sEPipctTxw)

The chat application I demoed can be found at https://github.com/djangobot/djangobot-example


Scott Burns

June 23, 2016


  1. An Introduction to Django Channels Scott Burns @scottsburns

  2. We’re hiring! Let’s talk after?

  3. WSGI • Web Server Gateway Interface • Defines the communication

    between applications/frameworks and servers. • PEP 333 — Dec. 2003 • Py3 addressed in PEP 3333 • Request→Response
  4. Django • “Batteries Included” Framework • Object-Relational Mapper • Templating

    • URL Routing • Signals, etc • Large 3rd-party ecosystem https://www.djangoproject.com/community/logos/
  5. Quick Demo

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

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

    WSGI App
  8. Client time Application HTTP 2

  9. None
  10. Client time Application WebSockets …

  11. None
  12. Challenges • Python async is improving quickly • Building a

    websocket-capable server is doable with autobahn, etc • How to actually build applications though?
  13. 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!
  14. Real application ? ?

  15. 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!)
  16. Interface Server (Daphne) ChannelLayer Worker Worker Worker Clients

  17. 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
  18. Channel Named first-in, first-out task queue Group Named, broadcast-able sets

    of channels
  19. 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”})
  20. HTTP View Request Response

  21. Messages Consumer Message Message Message

  22. $ pip install channels INSTALLED_APPS = [ …, channels, …,

  23. Let’s build a chat

  24. One room, all clients receive all messages

  25. One view to serve the page & assets

  26. 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.
  27. Demo

  28. Deployments

  29. Interface Server (Daphne) ChannelLayer Worker Worker Worker Clients Interface Server

    (Daphne) Worker Worker Worker Machine 1 Machine 2
  30. Interface Server (Daphne) ChannelLayer Worker Worker Worker Clients Interface Server

    (Daphne) Worker Worker Worker Machine 1 Machine 2 ChannelLayer
  31. Interface ChannelLayer Wor Wor Wor ChannelLayer Interface ChannelLayer Wor Wor

    Wor Interface ChannelLayer Wor Wor Wor (RedisLocalChannelLayer)
  32. Philosophically • It’s not just about WebSockets • Generalized eventing

    for Django • Many Protocols • Multi-framework backends?
  33. Links • Andrew Godwin’s 2016 PyCon Talk • Channels docs

    (channels.readthedocs.org) are fabulous • http://www.aeracode.org/2016/6/16/philosophy- channels/
  34. Questions?