Topic 20 / 40

WebSocket Notification Systems for Long-Running AI Tasks

~17 min read  //  Django Series  //  Coding India

1. Deep Architecture

Long-running AI jobs process asynchronously. Using Channel Layers, the worker processing a task publishes updates to a Redis group channel key. Uvicorn picks up the event and streams the update down the WebSocket to the client.

2. The Feynman Gatekeeper

[KNOWLEDGE CHECK] Trace the flow of a message sent from a Celery worker to a specific client browser connection via Redis Channel Layers.

3. The Code

# consumer.py
from channels.generic.websocket import AsyncJsonWebsocketConsumer

class EvaluationConsumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        self.group_name = f"video_{self.scope['url_route']['kwargs']['video_id']}"
        await self.channel_layer.group_add(self.group_name, self.channel_name)
        await self.accept()

    async def video_update(self, event):
        await self.send_json(event["content"])

4. The Funnel

Stat Level-Up: Broadcast Master (Lvl 1).
Sanjaya Integration: Update progress bars on the frontend as the video transcript is generated.