An ordered, first-in-first-out, at-most once queue with expiry and delivery to only one listening consumer. Identified by a unique unicode name. WHAT IS A CHANNEL?
There are standard channel names: STANDARDS Clients have unique response channels: http.request websocket.connect websocket.receive !http.response.jaS4kSDj !websocket.send.Qwek120d
There are standard message formats: STANDARDS - HTTP request - WebSocket close - HTTP response chunk But you can make your own channel names and formats too.
Channels are network-transparent, and protocol termination is separate from business logic process 1 process 2 protocol server worker server channels client socket
Async/cooperative code, but none of yours process 1 process 2 protocol server worker server channels client socket Your logic running synchronously worker server
EXAMPLE This notifies clients of new blog posts: def newpost(message): for client in client_list: Channel(client).send({ 'text': message.content['id'], })
EXAMPLE But Channels has a better solution for that: # Channel "websocket.connect" def connect(message): Group('liveblog').add( message.reply_channel) # Channel "new-liveblog" def newpost(message): Group('liveblog').send({ 'text': message.content['id'], })
EXAMPLE And you can send to channels from anywhere: class LiveblogPost(models.Model): ... def save(self): ... Channel('new-liveblog').send({ 'id': str(self.id), })
EXAMPLE All the routing is set up like URLs: from .consumers import ws_connect routing = { 'websocket.connect': ws_connect, 'websocket.receive': 'a.b.receive', 'thumbnail': 'images.consumers.thumb', }
EXAMPLE The HTTP channel just goes to the view system by default. routing = { # This is actually the default 'http.request': 'channels.asgi.ViewConsumer', }