Async Lab — WebSocket and Django Channels¶
Goal: explore the lifecycle of GroupChatConsumer, understand why database_sync_to_async is needed, and learn to test WebSocket with WebsocketCommunicator.
Related files: notes_app/consumers.py, notes_app/tests/test_consumers.py, notes_project/asgi.py.
Part 1 — Consumer lifecycle¶
Open notes_app/consumers.py and find class GroupChatConsumer(AsyncWebsocketConsumer).
For each of the three methods, describe in a comment what happens:
| Method | What it does | When it is called |
|---|---|---|
connect() |
During the WS handshake (before accept) | |
receive() |
When the browser has sent text | |
disconnect() |
When the connection is closed |
Task: find the line where the consumer closes the connection for an anonymous user. Which close code does it use? What does that code mean?
Part 2 — database_sync_to_async¶
Problem: the Django ORM is synchronous. In an async context, calling ChatMessage.objects.create(...) directly will raise:
Solution: database_sync_to_async runs the ORM in the Django thread pool:
from channels.db import database_sync_to_async
@database_sync_to_async
def _save_message(self, content):
return ChatMessage.objects.create(
group=self.group,
author=self.user,
content=content,
)
Questions:
- Why is
list(queryset.values(...))mandatory when returning data fromdatabase_sync_to_async? - What happens if you return a lazy QuerySet into the async context and access it later?
- Find the place in
consumers.pywhere the latest 50 messages are loaded. Which SQL operation runs?
Part 3 — WebsocketCommunicator¶
WebsocketCommunicator tests a consumer without a real browser or server.
Study the helper from test_consumers.py:
from channels.testing import WebsocketCommunicator
from notes_project.asgi import application
def _make_communicator(group_pk, user):
communicator = WebsocketCommunicator(
application,
f"/ws/groups/{group_pk}/chat/"
)
communicator.scope['user'] = user
communicator.scope['url_route'] = {'kwargs': {'group_pk': str(group_pk)}}
return communicator
Questions:
- Why set
scope['user']manually? Why doesAuthMiddlewareStacknot kick in during the test? - Why do consumer tests inherit from
TransactionTestCaserather thanTestCase?
Part 4 — Write your own test¶
Task: write a test that verifies that after a disconnect the consumer is correctly removed from the channel group (another participant does not receive messages for the disconnected one):
# notes_app/tests/test_consumers.py — add the method to the existing class
async def test_disconnected_user_does_not_receive_messages(self):
from channels.testing import WebsocketCommunicator
from notes_project.asgi import application
comm_alice = _make_communicator(self.group.pk, self.user1)
comm_bob = _make_communicator(self.group.pk, self.user2)
await comm_alice.connect()
await comm_bob.connect()
# Bob disconnects
await comm_bob.disconnect()
# Alice sends a message
await comm_alice.send_json_to({'content': 'Only for Alice'})
msg = await comm_alice.receive_json_from()
self.assertEqual(msg['content'], 'Only for Alice')
# Bob must NOT receive anything (he is not in the group)
# receive_nothing() returns True if there are no messages
self.assertTrue(await comm_bob.receive_nothing())
await comm_alice.disconnect()
Run it (with exec, not run):
docker compose up -d
docker compose exec web python manage.py test notes_app.tests.test_consumers -v 2
Part 5 — Channel Layer¶
Task: determine which channel layer is active in Docker and which one is active locally without Docker.
Explain: why InMemoryChannelLayer is not suitable for multi-worker production but is sufficient for a single Uvicorn process in development.
Part 6 — XSS in the WebSocket client¶
Open notes_app/static/notes_app/js/group_chat.js.
Find the escapeHtml function. Explain:
- Why message content must not be inserted directly via
innerHTML. - How
escapeHtmlprevents the execution of JavaScript code sent through the chat. - Is the server side protected? Where is
contentstored?
Summary¶
| Concept | Where in the code |
|---|---|
ProtocolTypeRouter |
notes_project/asgi.py |
AuthMiddlewareStack |
notes_project/asgi.py |
URLRouter + WS routes |
notes_project/routing.py |
GroupChatConsumer |
notes_app/consumers.py |
database_sync_to_async |
consumers.py — ORM wrapper |
channel_layer.group_send |
consumers.py — fan-out to everyone |
WebsocketCommunicator |
notes_app/tests/test_consumers.py |
TransactionTestCase |
test_consumers.py — not TestCase! |
socket_timeout=None |
notes_project/settings.py — Redis config |
| XSS escape | group_chat.js — escapeHtml() |
Evaluate and choose (analysis and evaluation level)¶
Q.1. WebSocket vs HTTP Polling: where is the line?¶
Notes Chat App uses WebSocket for the group chat.
Evaluate:
- Which feature in Notes Chat App could be implemented with polling, and would that be acceptable? (Reminders? The note list? Todo updates?)
- With 1000 active users, how many HTTP requests/sec does polling at a 2-second interval generate? How many with WebSocket?
- Name two scenarios where polling is better than WebSocket. Justify your answer.
- Find where the WS connection is established in group_chat.js. What happens on disconnect? Is there reconnect logic?
Q.2. InMemoryChannelLayer vs RedisChannelLayer: what to choose and when?¶
# Option A: In-Memory (no Redis)
CHANNEL_LAYERS = {
"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}
}
# Option B: Redis
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {"hosts": [REDIS_URL], "socket_timeout": None},
}
}
Evaluate:
- Imagine the production server runs 3 Uvicorn workers. What happens to the chat with Option A?
- With Option B, if Redis is unavailable, what happens to the chat?
- Find in settings.py which option is active when REDIS_URL is present and when it is not.
- Draw a conclusion: "Option A is appropriate when..., Option B is mandatory when..."
Q.3. async def view vs def view: where is async justified?¶
Django 5.2 supports async def views.
# Sync view
def note_list(request):
notes = selectors.get_user_notes(user=request.user)
return render(request, 'note_list.html', {'notes': notes})
# Async view
async def note_list(request):
# list() runs the SQL in the worker thread; a lazy QuerySet in render() → SynchronousOnlyOperation
notes = await database_sync_to_async(
lambda: list(selectors.get_user_notes(user=request.user))
)()
return render(request, 'note_list.html', {'notes': notes})
Evaluate:
- Why does Notes Chat App use sync views instead of async for most views?
- In which scenario does an async view give a real benefit for this project?
- What makes debugging async views harder compared to sync ones?
- Look through views.py: is there at least one async view? If not, why?