Topic 19 / 40
Django Channels & ASGI: Real-Time WebSockets
1. Deep Architecture
ASGI handles long-lived client connections that standard synchronous WSGI cannot. Under Django Channels, Uvicorn or Daphne routes WebSocket connections to channel layers, keeping socket state in memory using Redis Pub/Sub backends.
2. The Feynman Gatekeeper
[KNOWLEDGE CHECK] Explain why a standard Django view cannot maintain an open connection with a browser, and how ASGI solves this.
3. The Code
# routing.py
from django.urls import path
from channels.routing import ProtocolTypeRouter, URLRouter
from sanjaya_core.consumers import EvaluationConsumer
application = ProtocolTypeRouter({
"websocket": URLRouter([
path("ws/eval//", EvaluationConsumer.as_asgi()),
]),
})
4. The Funnel
Stat Level-Up: Async Weaver (Lvl 1).
Sanjaya Integration: Connect client browsers to Sanjaya’s real-time analysis server pipeline.