Real-Time Multiplayer Game Development Expert
Expert guidance for Claude Code when working with real-time multiplayer game engines implementing authoritative server architecture with client-side prediction, server reconciliation, and entity interpolation.
Core Multiplayer Architecture Principles
This skill provides expertise in Gabriel Gambetta's authoritative multiplayer architecture:
1. Authoritative Server
Server owns all game state to prevent cheatingClients send inputs (not state changes) to the serverServer validates inputs and broadcasts authoritative updatesAddresses network latency (50-500ms) challenges2. Client-Side Prediction
Clients immediately apply inputs locally for responsive gameplayEach input has a sequence number for trackingPending inputs stored in buffer for reconciliationOptimized for well-designed game mechanics3. Server Reconciliation Algorithm
Implement the following flow:
1. Client sends input with sequence number
2. Client applies input locally (prediction)
3. Server processes input and returns last processed sequence
4. Client receives server state update
5. Client discards acknowledged inputs
6. Client replays unacknowledged inputs from buffer
4. Entity Interpolation
Render other players 100ms in the past for smooth movementBuffer position updates for interpolationUse linear interpolation between known statesTrade-off: Visual delay for smoothness5. Debug Features
Ghost visualization for server's authoritative positionInterpolation visualization for buffered positionsReal-time stats: connection status, pending inputs, feature togglesProject Structure Pattern
When working with real-time multiplayer projects, expect this structure:
```
project/
├── client/ # Browser-based game client
│ ├── index.html # Main game UI with dual canvas (game + debug)
│ └── Client.js # Client with prediction/reconciliation
├── server/ # Node.js authoritative server
│ ├── Server.js # WebSocket server with game loop
│ └── package.json # Server dependencies (ws)
└── shared/ # Shared game logic
├── Entity.js # Game entity with position/movement
└── Input.js # Input data structure with serialization
```
Implementation Guidelines
Entity System Design
Use 2D position (x, y) with velocityImplement consistent movement speed (e.g., 200 pixels/second)Add canvas boundaries for movement constraintsMaintain position buffer for interpolationInput Processing Best Practices
Use delta-time based movement for frame-rate independenceImplement sequence numbering for reconciliationAdd input validation on server (e.g., max 100ms deltaTime)Normalize diagonal movement to prevent speed advantagesNetwork Protocol
Use WebSocket for real-time bidirectional communicationDefine clear message types: `connected`, `worldState`, `playerJoined`, `playerLeft`, `input`Run server at 10Hz update rate (adjustable)Run client at 60 FPS for smooth renderingDebug Visualization Features
Main canvas: Rendered game state with predictionDebug canvas: Ghost positions, interpolation points, real-time statsToggle controls for all networking featuresAdjustable interpolation delay (0-500ms)Development Workflow
Server Setup
```bash
cd server
npm install
npm start
```
Client Setup
Open `client/index.html` in a web browser. Support multiple simultaneous clients for multiplayer testing.
Code Modification Guidelines
When modifying real-time multiplayer code:
1. **Maintain Determinism**: Ensure entity movement is deterministic for accurate prediction
2. **Validate Server-Side**: Always validate all inputs on the server to prevent cheating
3. **Preserve Sequence Numbers**: Include sequence numbers in position updates for reconciliation
4. **Keep Shared Logic Shared**: Movement logic must be identical in client and server
5. **Test Network Conditions**: Verify behavior with prediction/reconciliation toggled on/off
Testing and Debugging
Implement real-time toggles for:
Client-side prediction (on/off)Server reconciliation (on/off)Entity interpolation (on/off)Interpolation delay slider (0-500ms)Ghost visualization (show server position overlay)These controls help developers understand how each technique affects gameplay under various network conditions.
Security and Anti-Cheat
Always remember:
Server is the source of truthClients send **inputs**, not state changesServer validates all client inputsPosition updates come from server onlyClient prediction is for UX, not authorityPerformance Considerations
Server update rate: Balance between bandwidth and responsiveness (10-60Hz typical)Client render rate: Aim for 60 FPSInput buffer size: Keep reasonable limit (e.g., 100 pending inputs max)Interpolation buffer: Store 2-3 position updates per entityDelta-time calculations: Ensure consistent across server/clientCommon Pitfalls to Avoid
1. **Client Authority**: Never trust client-reported positions
2. **Non-deterministic Logic**: Physics must produce same results on client and server
3. **Missing Reconciliation**: Always replay unacknowledged inputs
4. **Interpolation Artifacts**: Handle entity spawn/despawn edge cases
5. **Input Flooding**: Rate-limit client inputs server-side
When to Apply This Skill
Use this skill when:
Implementing real-time multiplayer game networkingDebugging client-server synchronization issuesOptimizing network prediction and reconciliationAdding new game mechanics to multiplayer systemsImplementing anti-cheat measuresTroubleshooting rubberbanding or desync issuesSetting up debug visualization for networking