Django Channels#
Installation#
pip install channels channels-redis
Setup#
# settings.py
INSTALLED_APPS = [
# ...
'channels',
]
ASGI_APPLICATION = 'myproject.asgi.application'
# Channel layers (Redis)
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
# Or in-memory (development)
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels.layers.InMemoryChannelLayer',
},
}
ASGI Configuration#
# myproject/asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import myapp.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
myapp.routing.websocket_urlpatterns
)
),
})
WebSocket Consumer#
# myapp/consumers.py
from channels.generic.websocket import AsyncWebsocketConsumer
import json
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = f'chat_{self.room_name}'
# Join room group
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
# Receive message from WebSocket
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
username = text_data_json['username']
# Send message to room group
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'chat_message',
'message': message,
'username': username
}
)
# Receive message from room group
async def chat_message(self, event):
message = event['message']
username = event['username']
# Send message to WebSocket
await self.send(text_data=json.dumps({
'message': message,
'username': username
}))
Routing#
# myapp/routing.py
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.ChatConsumer.as_asgi()),
]
Frontend (JavaScript)#
<!-- templates/chat.html -->
<script>
const roomName = '{{ room_name }}';
const chatSocket = new WebSocket(
'ws://' + window.location.host + '/ws/chat/' + roomName + '/'
);
chatSocket.onmessage = function(e) {
const data = JSON.parse(e.data);
const message = data.message;
const username = data.username;
// Display message
const messageElement = document.createElement('div');
messageElement.innerHTML = `<strong>${username}:</strong> ${message}`;
document.querySelector('#chat-log').appendChild(messageElement);
};
chatSocket.onclose = function(e) {
console.error('Chat socket closed unexpectedly');
};
// Send message
document.querySelector('#chat-message-input').onkeyup = function(e) {
if (e.keyCode === 13) {
document.querySelector('#chat-message-submit').click();
}
};
document.querySelector('#chat-message-submit').onclick = function(e) {
const messageInputDom = document.querySelector('#chat-message-input');
const message = messageInputDom.value;
chatSocket.send(JSON.stringify({
'message': message,
'username': '{{ user.username }}'
}));
messageInputDom.value = '';
};
</script>
Sync Consumer#
# myapp/consumers.py
from channels.generic.websocket import WebsocketConsumer
import json
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = f'chat_{self.room_name}'
# Join room group
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
)
self.accept()
def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name,
self.channel_name
)
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)
def chat_message(self, event):
message = event['message']
self.send(text_data=json.dumps({
'message': message
}))
Authentication#
# consumers.py
from channels.db import database_sync_to_async
from django.contrib.auth import get_user_model
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.user = self.scope['user']
if self.user.is_anonymous:
await self.close()
else:
await self.accept()
Background Tasks#
# consumers.py
from channels.db import database_sync_to_async
class NotificationConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
# Send notifications
await self.send_notifications()
@database_sync_to_async
def get_notifications(self):
return Notification.objects.filter(user=self.scope['user'])[:10]
async def send_notifications(self):
notifications = await self.get_notifications()
for notification in notifications:
await self.send(text_data=json.dumps({
'type': 'notification',
'message': notification.message
}))
Redis Setup#
# Install Redis
# Windows: Download from https://redis.io/download
# Linux: sudo apt-get install redis-server
# Mac: brew install redis
# Start Redis
redis-server
# Or use Docker
docker run -p 6379:6379 redis
Next: Django Signals