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"
Slide 26
Slide 26 text
Server Server Django Script
ASGI ASGI ASGI ASGI
Redis
Slide 27
Slide 27 text
Server Server Django Script
ASGI ASGI ASGI ASGI
Shared
Memory
Slide 28
Slide 28 text
Server Server Django Script
ASGI ASGI ASGI ASGI
Shared
Memory
Redis Redis
Slide 29
Slide 29 text
bit.ly/asgi-spec
Slide 30
Slide 30 text
Channels wraps the low-level
ASGI operations
Slide 31
Slide 31 text
Think of it as the "Django bit"
of a larger whole.
Slide 32
Slide 32 text
Daphne
HTTP/WebSocket Server
Channels
Django integration
asgi-redis
Redis backend
asgi-ipc
Local memory backend
asgiref
Shared code and libs
Slide 33
Slide 33 text
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
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
Slide 38
Slide 38 text
Full worked example
github.com/andrewgodwin/channels-examples
Slide 39
Slide 39 text
Ignoring Django
Slide 40
Slide 40 text
1. Take a channel layer
daphne myproject.asgi:channel_layer
Slide 41
Slide 41 text
2. Tie it into an event loop
Twisted, asyncio, or while-True
Slide 42
Slide 42 text
3. Call send/receive
It's a communication channel!
Slide 43
Slide 43 text
Example: SOA
Services receive()-block waiting for tasks
Clients use send() with a reply-channel
to call an endpoint
Servers process and send() the reply