Socket.IO Events Reference
ClassQuiz uses Socket.IO for real-time communication between the game admin (teacher) and players during quiz gameplay. This page documents all events, their payloads, and usage patterns.Connection
Socket.IO server is mounted at the root path and uses ASGI mode:Event Flow Overview
Game Lifecycle
Connection Events
connect
Direction: Server → Client (automatic)
When: Client establishes Socket.IO connection
Handler:
session_id event with session ID
session_id
Direction: Server → Client
Payload:
Admin Events
These events are used by the quiz admin (teacher) to control the game.register_as_admin
Direction: Client → Server
Purpose: Register the client as the game admin
Payload:
registered_as_admin- Successalready_registered_as_admin- Another admin already existserror- Validation error
registered_as_admin
Direction: Server → Client
Payload:
start_game
Direction: Client → Server
Purpose: Start the game and allow players to begin
Payload: Empty object {}
Authorization: Must be admin
Implementation:
start_game event sent to all players in the game room
set_question_number
Direction: Client → Server
Purpose: Display a specific question to all players
Payload: String representation of question index
ABCD- Multiple choiceRANGE- Numeric rangeVOTING- Vote for optionsSLIDE- Information slide (no answer)TEXT- Text inputORDER- Order items correctlyCHECK- Multiple correct answers
get_question_results
Direction: Client → Server
Purpose: Retrieve and display results for a specific question
Payload:
question_results
question_results
Direction: Server → Client (broadcast)
Payload:
show_solutions
Direction: Client → Server
Purpose: Display the correct answers to all players
Payload: Empty object {}
Authorization: Must be admin
Implementation:
solutions with full question data including correct answers
get_final_results
Direction: Client → Server
Purpose: Get complete game results with all questions
Payload: Empty object {}
Authorization: Must be admin
Implementation:
final_results
Payload Format:
kick_player
Direction: Client → Server
Purpose: Remove a player from the game
Payload:
kick event (signals they were removed)
save_quiz
Direction: Client → Server
Purpose: Save game results to persistent storage
Payload: Empty object {}
Authorization: Must be admin
Implementation:
results_saved_successfully event
get_export_token
Direction: Client → Server
Purpose: Get a token to export game results
Payload: None
Authorization: Must be admin
Implementation:
export_token event with token string (expires in 2 hours)
Player Events
These events are used by players participating in the quiz.join_game
Direction: Client → Server
Purpose: Join a game with a username and game PIN
Payload:
- Game must exist
- Game must not be started
- Username must be unique in the game
- Captcha required if
captcha_enabledis true
joined_game- Successgame_not_found- Invalid game PINgame_already_started- Game in progressusername_already_exists- Username takenerror- Validation error
- Admin receives
player_joinedevent
joined_game
Direction: Server → Client
Payload: Game data without sensitive information
rejoin_game
Direction: Client → Server
Purpose: Reconnect to a game after disconnection
Payload:
rejoined_game event with current game state
submit_answer
Direction: Client → Server
Purpose: Submit an answer for the current question
Payload:
player_answer- Acknowledgementalready_replied- Already submitted for this questioneveryone_answered- All players have answered (broadcast)
player_joined
Direction: Server → Client (admin only)
Purpose: Notify admin that a player joined
Payload:
Synchronization Events
time_sync
Direction: Server → Client
Purpose: Synchronize time between server and client for accurate scoring
Payload: Encrypted ISO timestamp string
Implementation:
echo_time_sync with same encrypted string
echo_time_sync
Direction: Client → Server
Purpose: Echo encrypted timestamp back to calculate latency
Payload: The encrypted timestamp received in time_sync
Implementation:
Remote Control Events
register_as_remote
Direction: Client → Server
Purpose: Register as remote controller (e.g., mobile device controlling presentation)
Payload:
set_control_visibility
Direction: Client → Server
Purpose: Show/hide controls on admin screen (for remote control mode)
Payload:
control_visibility event to admin room
Error Events
error
Direction: Server → Client
Payload: None or error details
When: Validation errors, malformed data, or unexpected errors
game_not_found
Direction: Server → Client
Payload: None
When: Invalid game PIN provided
game_already_started
Direction: Server → Client
Payload: None
When: Player tries to join a game that’s already in progress
username_already_exists
Direction: Server → Client
Payload: None
When: Player tries to join with a username already in use
already_registered_as_admin
Direction: Server → Client
Payload: None
When: Another admin is already registered for the game
already_replied
Direction: Server → Client
Payload: None
When: Player tries to submit multiple answers for the same question
kick
Direction: Server → Client
Payload: None
When: Player was kicked by admin
Room System
Socket.IO rooms organize clients:Game Room
- Name:
{game_pin}(e.g.,"45823169") - Members: All players + admin
- Purpose: Broadcast game events to everyone
Admin Room
- Name:
admin:{game_pin}(e.g.,"admin:45823169") - Members: Game admin(s) only
- Purpose: Admin-specific notifications (e.g., player joins)
Redis Data Structure
Socket.IO uses Redis for state management:Client Implementation Example
Best Practices
Error Handling
Always handle error events:Reconnection
Implement reconnection logic:Time Synchronization
Always respond to time_sync for accurate scoring:Authorization
The server checks authorization for admin events:- Events are ignored if
session["admin"]is notTrue - No error is returned to prevent information disclosure