2. Django awarded MOSS
Grant
We've been awarded $150,000 to help fund the
development of Channels, the initiative to rewrite the
core of Django to support (among other things)
WebSockets and background tasks, and to integrate
key parts of the Django REST Framework request/
response code into Django, such as content
negotiation.
11. Channels can’t do
broadcast
redis_conn = redis.Redis("localhost", 6379)
@receiver(post_save, sender=BlogUpdate)
def send_update(sender, instance, **kwargs):
# Loop through all response channels and send the update
for reply_channel in redis_conn.smembers("readers"):
Channel(reply_channel).send(
id=instance.id,
content=instance.content,
)
# Connected to websocket.connect
def ws_connect(message):
# Add to reader set
redis_conn.sadd("readers", message.reply_channel.name)
12. With Groups
@receiver(post_save, sender=BlogUpdate)
def send_update(sender, instance, **kwargs):
Group("readers").send(
id=instance.id,
content=instance.content,
)
# Connected to websocket.connect and websocket.keepalive
def ws_connect(message):
# Add to reader group
Group("readers").add(message.reply_channel)
17. from channels import Group
# Connected to websocket.connect and websocket.keepalive
def ws_add(message):
Group("chat").add(message.reply_channel)
# Connected to websocket.receive
def ws_message(message):
Group("chat").send(message.content)
# Connected to websocket.disconnect
def ws_disconnect(message):
Group("chat").discard(message.reply_channel)
consumers.py
21. Linearization
The linearize decorator. Any handler decorated with this will
use locking to ensure it does not run at the same time as
any other view with linearize on messages with the same
reply channel.
23. Authentication
from channels.auth import (
http_session_user, channel_session_user, transfer_user
)
@channel_session
@http_session_user
def ws_add(message):
# Copy user from HTTP to channel session
transfer_user(message.http_session, message.channel_session)
# Add them to the right group
Group(
"chat-%s" % message.user.username[0]
).add(message.reply_channel)
@channel_session_user
def ws_keepalive(message):
# Keep them in the right group
Group(
"chat-%s" % message.user.username[0]
).add(message.reply_channel)