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

A Brief History of Channels

A Brief History of Channels

A talk I gave at DjangoCon Europe 2016

Andrew Godwin

March 31, 2016

More Decks by Andrew Godwin

Other Decks in Programming


  1. Andrew Godwin @andrewgodwin BRIEF HISTORY of CHANNELS A

  2. Andrew Godwin Hi, I'm Django core developer Senior Software Engineer

    at Likes networking a little bit too much
  3. Channels

  4. Channels?

  5. WebSocket

  6. HTTP 1 request response Browser Server request response request response

    request response
  7. HTTP 2 request response Browser Server request response request 1

    response 2 request 2 response 1
  8. WSGI request response Browser Server Django call app return val

  9. WebSockets receive send Browser Server send receive send send

  10. receive send Browser Server send receive send send Django ?

  11. Hard to deadlock Built-in authorisation Scales down Widely deployable Optional

  12. Django is not built for this.

  13. But what if it was?

  14. Instead of requests, we have events

  15. Events of the same type are grouped on a named

  16. HTTP requests HTTP responses WebSocket connecteds WebSocket frames received WebSocket

    frames sent WebSocket disconnections
  17. A channel is a: named, first-in-first-out, at-most-once, non-broadcast, network-transparent queue

    of messages
  18. http.request Protocol Server Worker Server http.response websocket.connect websocket.send websocket.receive websocket.send

  19. Responses are on single-reader channels (http.response!1A2B3C)

  20. You can send onto channels from anywhere

  21. How do you use it?

  22. View request response callable

  23. Consumer event event callable event event

  24. Every message on a channel runs the consumer function

  25. http.request routes to a view system consumer

  26. def ws_message(message): # Get things from message data = json.loads(message['text'])

    # Use ORM like normal MyModel.objects.create( value=data['value'], ) # Send a reply and close socket message.reply_channel.send({ 'text': 'OK', 'close': True, })
  27. routing.py: urls.py for Channels

  28. message.user: like request.user

  29. message.channel_session: per-socket sessions

  30. Groups broadcast/pub-sub

  31. Add a channel to a Group Remove a channel from

    a Group Send to a Group
  32. Worked Examples github.com/andrewgodwin/channels-examples

  33. Liveblog http.request Django view layer websocket.connect Add to liveblog Group

    websocket.disconnect Remove from liveblog Group <Article.save> Send notification to liveblog
  34. Chat http.request Django view layer websocket.receive Either add to room

    Group or send to chat_messages websocket.disconnect Remove from room Groups chat_messages Send to room Groups
  35. It's so easy to join! CALL NOW

  36. Things I didn't even get to: Replacing WSGI Pluggable channel

    backends Sharding and scaling It's still just runserver
  37. A base for the future Scheduler? Retry logic? Generic Consumers?

    ...and much more
  38. 1.10 "Provisional"?

  39. 1.8 1.9 pip install channels

  40. channels.readthedocs.com github.com/andrewgodwin/channels-examples

  41. Thanks. Andrew Godwin @andrewgodwin