Connection
Connect to the Socket.IO server at the same host and port as the REST API (default:http://localhost:3001).
The server accepts connections from any origin (
cors: { origin: '*' }). Configure CORS appropriately for production environments.Client Events
Events emitted by clients to the server.create_room
Creates a new game room and assigns the creator as the host with a unique 6-digit room code.Callback Response
Always
true for successful room creation.The 6-digit room code (e.g., “123456”).
Player’s role in the room. Always
"host" for the creator.The host starts with the first turn. The game state initializes with
currentTurn: 'host' and turnNumber: 0.join_room
Joins an existing game room using a 6-digit code. The player is assigned the “guest” role.Parameters
The 6-digit room code to join. Whitespace is automatically stripped.
Callback Response
true if successfully joined, false otherwise.The room code (only present on success).
Player’s role in the room. Always
"guest" when joining (only present on success).Error message (only present on failure).
game_event
Generic event for sending game actions to the opponent. The server relays the event to all other players in the same room.Parameters
The payload can contain any data structure. Common fields include:The type of game event (e.g., “card_played”, “attack”, “defense”).
Event-specific data. Structure depends on the event type.
The server does not validate or modify the payload. It simply relays it to other players in the room. Implement validation in your game logic.
end_turn
Signals the end of the current player’s turn and switches the active player.Parameters
Optional data about the turn. Not used by the server but can be useful for logging.
Behavior
The server automatically:- Alternates the
currentTurnbetween “host” and “guest” - Increments the
turnNumbercounter - Broadcasts a
turn_changedevent to both players
Turn switching is automatic based on the sender’s role. The host’s turn ends → guest’s turn begins, and vice versa.
Server Events
Events emitted by the server to clients.room_created
Emitted to the room when it’s successfully created.Payload
The 6-digit room code.
player_joined
Emitted to all players in a room when a new player joins.Payload
Current number of players in the room (1 or 2).
true when the room has 2 players and the game can begin.game_start
Emitted when the room reaches 2 players and the game begins. Both players receive this event simultaneously.Payload
The role of the player whose turn it is. Always
"host" at game start.The socket ID of the host player.
The socket ID of the guest player.
Use the socket IDs to identify which player you are. Compare
socket.id with hostId and guestId to determine your role.game_event
Received when the opponent sends a game action.Payload
The payload structure matches what the opponent sent. The server does not modify the data.Events are only sent to other players in the room, not back to the sender.
turn_changed
Emitted to both players when a turn ends and the active player switches.Payload
The role of the player whose turn it now is (“host” or “guest”).
The current turn number. Increments with each turn change, starting from 0.
player_left
Emitted when a player disconnects from the room.Room Lifecycle
- Creation: Host creates room → receives
room_createdevent - Joining: Guest joins → both players receive
player_joinedevent - Game Start: When 2 players present → both receive
game_startevent - Gameplay: Players exchange
game_eventandend_turnevents - Disconnection: Player leaves → remaining player receives
player_leftevent - Cleanup: Last player leaves → room is deleted
Game State Structure
The server maintains the following state for each room:The server-side game state is minimal by design. Game logic, card state, and board state are managed client-side. The server only facilitates communication and turn management.
Implementation Reference
Socket event handling is implemented inBackend/socketManager.js:
- Room creation:
socketManager.js:15-37 - Joining rooms:
socketManager.js:39-78 - Game events:
socketManager.js:81-89 - Turn management:
socketManager.js:92-110 - Disconnection:
socketManager.js:112-124