Skip to main content

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:

  1. Caddy (Webserver): The entry point. Handles HTTPS termination and serves static files.
  2. Web (Django/Daphne): The application logic. Handles HTTP requests (Admin) and WebSockets (Game).
  3. Redis: The communication layer. It acts as the "glue" that allows Django instances to send messages to connected WebSocket clients.
  4. 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:

  1. Handshake: The browser initiates a WebSocket handshake at /ws/game/<ROOM_CODE>/.
  2. Routing: routing.py directs this to consumers.GameConsumer.
  3. Channel Layer: The consumer adds the user's connection ID to a "Room Group" stored in Redis.
  4. Broadcast: When the Host clicks "Next Question", Django sends a message to the Redis Group, and Redis distributes it to all connected players instantly.