Task 4 — Django Channels consumer: websocket connection, auth, message events
Title: realtime: implement WebSocket consumer(s) for messaging using Django Channels
API / behavior
-
WebSocket endpoint: ws://<host>/ws/chat/
-
Authentication: accept JWT in Sec-WebSocket-Protocol or use session cookie (documented).
-
On connect: authenticate user, subscribe to channels for user (user.<userid>) and to conversation groups when requested.
-
Events:
join_conversation { "conversation_id": "uuid" } → subscribe group conversation.<id>.
leave_conversation
send_message { "conversation_id","body","attachments":[upload_ids] } → validate, call service.create_message, publish message_received.
typing and presence signals forwarded to other participants.
mark_read { "conversation_id" } → call service.mark_messages_read and emit read_receipt.
-
Outgoing events:
message_received (to conversation group) with message payload.
read_receipt, typing, presence events.
Acceptance
Dependencies
- Redis channel layer (Channels).
- Messaging service (Task 3).
- Auth middleware for Channels.
Notes
- Keep consumer logic thin: validate + call service + publish event.
- Ensure proper exception handling to avoid leaking info.
Task 4 — Django Channels consumer: websocket connection, auth, message events
Title: realtime: implement WebSocket consumer(s) for messaging using Django Channels
API / behavior
WebSocket endpoint:
ws://<host>/ws/chat/Authentication: accept JWT in
Sec-WebSocket-Protocolor use session cookie (documented).On connect: authenticate user, subscribe to channels for user (
user.<userid>) and to conversation groups when requested.Events:
join_conversation{ "conversation_id": "uuid" }→ subscribe groupconversation.<id>.leave_conversationsend_message{ "conversation_id","body","attachments":[upload_ids] }→ validate, call service.create_message, publishmessage_received.typingandpresencesignals forwarded to other participants.mark_read{ "conversation_id" }→ call service.mark_messages_read and emitread_receipt.Outgoing events:
message_received(to conversation group) with message payload.read_receipt,typing,presenceevents.Acceptance
Consumer unit tests (channels testing) that:
Dependencies
Notes