System Architecture
TriviaFlow is a real-time application that relies on the ASGI (Asynchronous Server Gateway Interface) standard to handle WebSockets.
Component Overview
The system is composed of four main Docker containers:
- Caddy (Webserver): The entry point. Handles HTTPS termination and serves static files.
- Web (Django/Daphne): The application logic. Handles HTTP requests (Admin) and WebSockets (Game).
- Redis: The communication layer. It acts as the "glue" that allows Django instances to send messages to connected WebSocket clients.
- MariaDB: Persists quizzes, sessions, and player scores.
Data Flow
1. HTTP Requests (Admin & Views)
Standard requests (like opening the dashboard) follow the traditional Django path:
Caddy -> Daphne -> urls.py -> views.py -> HTML Response.
2. WebSocket Connections (Real-time)
When a player joins:
- Handshake: The browser initiates a WebSocket handshake at
/ws/game/<ROOM_CODE>/. - Routing:
routing.pydirects this toconsumers.GameConsumer. - Channel Layer: The consumer adds the user's connection ID to a "Room Group" stored in Redis.
- Broadcast: When the Host clicks "Next Question", Django sends a message to the Redis Group, and Redis distributes it to all connected players instantly.