Adding Multiplayer to Browser Games: WebSocket Basics
Multiplayer is the most requested feature for browser games — and the hardest to implement. Here's a practical guide that skips the theory and focuses on what actually works.
Architecture Decision: Who's the Authority?
Before writing any code, decide:
- Authoritative server: The server decides what's "real." Clients send inputs, server processes them, sends back results. Harder to hack, more complex to build, required for competitive games
- Peer-to-peer (via relay): One client is the "host," others connect through a relay server. Simpler, but the host can cheat. Fine for co-op and casual games
For most browser games, start with an authoritative server. It's more work upfront but saves you from cheating nightmares later.
WebSocket Basics
WebSockets provide a persistent, bidirectional connection between client and server. Unlike HTTP, the connection stays open, so messages can flow in both directions instantly.
On the client side, it's simple: create a WebSocket connection, send JSON messages, and handle incoming messages. The complexity lives on the server.
Server-Side Implementation
Game Loop on the Server
Your server runs its own game loop — typically at a lower tick rate than the client's frame rate. Common tick rates:
- 10 ticks/second: Turn-based, casual, and strategy games
- 20-30 ticks/second: Action games, platformers, RPGs
- 60+ ticks/second: Fast-paced shooters, fighting games (rare for browser games)
State Synchronization
The server sends game state to clients. Two approaches:
- Full state snapshots: Send the entire game state every tick. Simple but bandwidth-heavy. Works for small games (under 20 entities)
- Delta compression: Only send what changed since last tick. More complex but uses 90% less bandwidth. Required for larger games
Latency Compensation
The biggest challenge in multiplayer. A player 200ms away from your server sees the game 200ms in the past. Three techniques to handle this:
- Client-side prediction: The client predicts the result of its own inputs immediately, then corrects if the server disagrees. Makes the game feel responsive despite latency
- Server reconciliation: When the server's state conflicts with the client's prediction, smoothly interpolate to the correct state instead of snapping
- Entity interpolation: Other players are rendered slightly in the past (one tick behind) to ensure smooth movement even with jittery connections
Hosting Options for Browser Game Servers
- Fly.io: Deploy Node.js WebSocket servers globally, free tier available. Best for getting started
- Cloudflare Durable Objects: Serverless WebSocket with state persistence. Great for room-based games
- Hathora / Rivet: Game-specific hosting platforms with matchmaking built in
- Self-hosted VPS: A $5/month DigitalOcean droplet handles 50-100 concurrent players
Start Simple
Don't build an MMO. Start with:
- A WebSocket echo server that confirms connections
- A shared cursor — each player sees everyone's mouse position
- A simple game room — two players take turns
- Real-time movement with interpolation
Each step teaches a core concept. By step 4, you have the foundation for any multiplayer browser game.