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

Architecting with Channels

Architecting with Channels

My keynote from DjangoCon US 2016.

Andrew Godwin

July 19, 2016
Tweet

More Decks by Andrew Godwin

Other Decks in Programming

Transcript

  1. 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"
  2. 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
  3. Routing from channels import route routing = [ route("websocket.connect", on_connect),

    route("websocket.receive", on_receive), route("websocket.disconnect", on_disconnect) ]
  4. 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
  5. 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"]
  6. 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
  7. 1995 You are a desktop app 2005 You are a

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