Architecture Overview
The multiplayer system is built on a WebSocket-based client-server architecture that provides low-latency, bidirectional communication.WebSocket Server
Port 3002 handles all game connections and real-time move synchronization
REST API
Port 3001 manages user authentication, game history, and persistent data
Client Handler
React-based WebSocket handler manages connection state and packet routing
Game Instance
Synchronized game state across all connected clients
WebSocket Connection
Establishing Connection
TheWebsocketHandler class manages the client-side WebSocket connection:
src/app/game_mode/WebsocketHandler.ts
Connection Promise
The handler provides an async connection method that resolves when the WebSocket is ready:src/app/game_mode/WebsocketHandler.ts
1
Initialize Handler
Create a new
WebsocketHandler instance with callback functions for each event type.2
Await Connection
Use
awaitConnection() to ensure the WebSocket is fully established before sending packets.3
Send Packets
Use
sendPacket() to transmit game actions to the server.4
Handle Responses
The handler automatically routes incoming packets to the appropriate callback functions.
Packet System
BeeHex uses a strongly-typed packet system for all client-server communication.Server-Bound Packets
Packets sent from client to server:- GAME_SEARCH
- JOIN_ROOM
- PLAY_MOVE
- FORFEIT_GAME
Request matchmaking with specific game parameters:Usage:
src/app/definitions.ts
Client-Bound Packets
Packets sent from server to client:ERROR_MESSAGE
ERROR_MESSAGE
Error notifications from the server:
src/app/definitions.ts
GAME_SEARCH
GAME_SEARCH
Matchmaking status updates:Provides real-time information about:
src/app/definitions.ts
- Number of players currently searching
- The ELO range being considered for matches
- Confirmed game parameters
GAME_FOUND
GAME_FOUND
Notification when a match is found:The client automatically redirects to the game page using this ID.
src/app/definitions.ts
JOIN_GAME
JOIN_GAME
Complete game state when joining:This packet provides all information needed to initialize the game board.
src/app/definitions.ts
MOVE_PLAYED
MOVE_PLAYED
Real-time move synchronization:Sent to all spectators and the opponent whenever a move is played.
src/app/definitions.ts
GAME_END
GAME_END
Game conclusion with complete data:Includes:
src/app/definitions.ts
- Winner determination
- Complete move sequence
- Winning path coordinates for visualization
Packet Handler Implementation
The WebSocket handler routes incoming packets to appropriate callbacks:src/app/game_mode/WebsocketHandler.ts
Game Flow
Complete Multiplayer Lifecycle
1
Connection
Player establishes WebSocket connection to game server.
2
Matchmaking
Client sends GAME_SEARCH or JOIN_ROOM packet with desired parameters.Server responds with periodic GAME_SEARCH updates showing matchmaking status.
3
Game Start
Server sends GAME_FOUND with unique game ID.Client navigates to game page:
/hex/o_{game_id}Server sends JOIN_GAME with complete initial state.4
Active Gameplay
Players alternate sending PLAY_MOVE packets.Server validates moves and broadcasts MOVE_PLAYED to all participants.
src/app/hex/[gameId]/page.tsx
5
Game End
Server detects win condition and sends GAME_END packet.Client displays victory/defeat screen and enters review mode.Game data is saved to database with final status and MMR changes.
Reconnection Handling
BeeHex implements automatic reconnection for network interruptions:src/app/hex/[gameId]/page.tsx
When reconnecting, the client automatically rejoins the game using the stored game ID and synchronizes to the current state.
User Status Management
The server tracks each player’s current status:src/app/definitions.ts
Player Information
The system fetches and displays player information for each match:src/app/hex/[gameId]/page.tsx
- Username
- Current MMR (for ranked games)
- Player status
- Timer (when implemented)
Spectator Support
While not fully implemented in the current version, the packet system is designed to support spectators:src/app/definitions.ts
Security Considerations
Server-Side Validation
Server-Side Validation
All moves are validated on the server before broadcasting:
- Move legality checking
- Turn order enforcement
- Game state verification
- Player authentication
Client-Side Checks
Client-Side Checks
The client performs preliminary validation to provide immediate feedback:However, the server has final authority on all game actions.
Database Integration
Completed games are persisted to the database:src/app/definitions.ts
- Game history viewing
- MMR calculation and tracking
- Post-game analysis
- Statistics and leaderboards
Error Handling
The multiplayer system includes comprehensive error callbacks:src/app/hex/[gameId]/page.tsx
- Invalid game ID
- Connection timeout
- Invalid move attempts
- Authentication failures
- Server unavailability
Next Steps
Game Modes
Learn about different ways to play BeeHex
AI Analysis
Explore the move evaluation engine