Architecting with Channels

Architecting with Channels

My keynote from DjangoCon US 2016.

077e9a0cb34fa3eba2699240c9509717?s=128

Andrew Godwin

July 19, 2016
Tweet

Transcript

  1. 1.
  2. 2.
  3. 3.
  4. 6.
  5. 7.
  6. 8.
  7. 10.
  8. 11.
  9. 16.
  10. 21.
  11. 29.

    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"
  12. 37.

    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
  13. 40.

    Routing from channels import route routing = [ route("websocket.connect", on_connect),

    route("websocket.receive", on_receive), route("websocket.disconnect", on_disconnect) ]
  14. 41.

    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
  15. 43.
  16. 44.
  17. 46.

    class IntegerValue(models.Model): name = models.CharField(max_length=100, unique=True) value = models.IntegerField(default=0) class

    IntegerValueBinding(WebsocketBinding): model = IntegerValue stream = "intval" def check_permission(self, user, action, instance): return True def group_names(self, instance, action): return ["binding.values"]
  18. 51.

    Write a custom server Use asyncio, Twisted, Gevent, etc Server

    takes path to ASGI channel layer instance (CLI/config file) Roll layer.send, layer.receive_many into your event loop
  19. 56.

    1995 You are a desktop app 2005 You are a

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