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
Tweet

More Decks by Scott Burns

Other Decks in Technology

Transcript

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

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

    • URL Routing • Signals, etc • Large 3rd-party ecosystem https://www.djangoproject.com/community/logos/
  3. Challenges • Python async is improving quickly • Building a

    websocket-capable server is doable with autobahn, etc • How to actually build applications though?
  4. 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!
  5. 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!)
  6. 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
  7. 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.
  8. Interface Server (Daphne) ChannelLayer Worker Worker Worker Clients Interface Server

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

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

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

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