A FastAPI-based backend for the Quizzler online quiz platform with real-time live quiz functionality. Built for scalability and supporting 100+ concurrent users.
- User Authentication: Secure signup/login with JWT tokens
- Quiz Management: Create, edit, and manage quizzes
- Quiz Sessions: Start and manage quiz sessions
- Results & Analytics: Comprehensive results and leaderboards
- Live Quiz Hosting: Real-time quiz sessions with WebSocket connections
- Multi-room Support: Up to 100 concurrent quiz rooms
- Scalable Architecture: Supports 50+ players per room
- Real-time Leaderboards: Live scoring with time-based bonuses
- Connection Management: Automatic cleanup and heartbeat monitoring
- Production Ready: Built for 100+ concurrent users
# 1. Clone the repository
git clone <repository-url>
cd quizzler/backend
# 2. Create and activate virtual environment
python -m venv quizzler_env
source quizzler_env/bin/activate
# 3. Install dependencies
pip install -r requirements.txt
# 4. Set up environment variables
cp .env.example .env
# Edit .env with your Supabase credentials
# 5. Create database tables
# Run the SQL from create_tables.sql in your Supabase dashboard
# 6. Run the application
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000SUPABASE_URL=your_supabase_url
SUPABASE_KEY=your_supabase_anon_key
SUPABASE_SERVICE_KEY=your_supabase_service_key
JWT_SECRET_KEY=your_jwt_secret
DATABASE_URL=postgresql://your_db_connection
# Real-time System Configuration (Optional)
MAX_ROOMS=100
MAX_PLAYERS_PER_ROOM=50
MAX_CONNECTIONS_PER_IP=5
HEARTBEAT_INTERVAL=30
SESSION_MAX_AGE=7200POST /auth/signup- Register a new userPOST /auth/signin- Login and get JWT tokenPOST /auth/signout- Logout userGET /auth/me- Get current user infoPOST /auth/verify-token- Verify JWT token
POST /quizzes- Create a new quizGET /quizzes- Get user's quizzesGET /quizzes/{quiz_id}- Get specific quizGET /quizzes/trivia- Get trivia questionsGET /quizzes/topics/list- Get available topics
POST /quizzes/{quiz_id}/start- Start a quiz sessionPOST /quizzes/{quiz_id}/submit- Submit quiz answers
GET /results/{quiz_id}/my-result- Get personal resultsGET /results/{quiz_id}/results- Get quiz results (host)GET /results/leaderboards/global- Global leaderboardGET /results/leaderboards/quiz/{quiz_id}- Quiz leaderboardGET /results/stats/user- User statistics
WS /realtime/ws/host/{room_code}?token=Bearer_TOKEN- Host connection (authenticated)WS /realtime/ws/player/{room_code}?username=PLAYER_NAME- Player connection (public)
GET /realtime/rooms/validate/{room_code}- Validate room existsGET /realtime/rooms/{room_code}/stats- Get room statisticsGET /realtime/health- System health checkPOST /realtime/admin/cleanup- Force cleanup inactive sessions
- Concurrent Rooms: 100 rooms simultaneously
- Players per Room: 50 players maximum
- Total Concurrent Users: 5,000+ players
- Message Throughput: 1,000+ messages/second
- Memory Usage: <1GB RAM at full capacity
- Response Time: <100ms average
- Automatic Cleanup: Removes inactive sessions every 5 minutes
- Heartbeat Monitoring: Connection health checks every 30 seconds
- Error Recovery: Graceful handling of connection failures
- Rate Limiting: 30 requests/minute per IP address
- Memory Management: Real-time resource monitoring
- Health Checks:
/realtime/healthendpoint for monitoring
GET /realtime/healthResponse:
{
"healthy": true,
"active_rooms": 5,
"total_players": 127,
"memory_usage_mb": 245.6,
"cpu_percent": 15.2,
"metrics": {
"total_connections": 132,
"messages_sent": 5847,
"errors": 2,
"questions_processed": 45,
"answers_processed": 892
},
"limits": {
"max_rooms": 100,
"max_players_per_room": 50,
"max_connections_per_ip": 5
}
}- Connection Success Rate: >99%
- Average Response Time: <100ms
- Memory Efficiency: <1GB for 5000 users
- Error Rate: <0.1%
- Uptime: 99.9%+ target
- JWT Tokens: Secure user authentication
- Role-Based Access: Host vs player permissions
- Token Validation: Real-time token verification
- Session Management: Secure session handling
- Rate Limiting: Prevents spam and abuse
- Connection Limits: Per-IP connection restrictions
- Input Validation: All WebSocket messages validated
- CORS Configuration: Proper cross-origin setup
- Error Isolation: Individual connection failures don't cascade
- FastAPI: Modern web framework
- WebSockets: Real-time communication
- Pydantic: Data validation and modeling
- SQLAlchemy: Database ORM
- Supabase: Database and authentication
- psutil: System monitoring
- python-jose: JWT handling
This project is licensed under the MIT License.