Live ghost-racing multiplayer (design research)
multiplayerwebsocketnetworkingdartghostnetcode
Research (2026-04, not built) on adding real-time multiplayer to a Flutter/Flame platformer in the cheapest way: players race the same level and see each other as non-colliding ghosts. The key insight is that ghost racing avoids almost every hard problem of multiplayer.
Why ghost racing is dramatically simpler
| Aspect | Full multiplayer | Ghost racing |
|---|---|---|
| Physics | authoritative or deterministic | each client independent |
| Collision between players | must sync | none |
| Latency sensitivity | high | low (100-250 ms is fine) |
| Server | full simulation | dumb relay |
| Desync | reconciliation | irrelevant |
| Effort | 6-12 weeks | 2-4 weeks solo part-time |
| Skip client prediction, server reconciliation, rollback netcode, input syncing, and delta | ||
| compression; none are needed when remote players are only visual. |
Transport
- WebSockets: work in Flutter web and natively (
web_socket_channel). Use this. - WebRTC: overkill (P2P media, NAT traversal). Raw UDP (
enet): no browser support. Polling: too slow. Peer-to-peer in general: NAT pain, avoid. - PHP is a poor fit for long-lived sockets (Swoole/OpenSwoole need a PECL extension and a different programming model; Ratchet is slow). Keep PHP for REST.
Server options
- Dart relay server (recommended):
dart:ioHttpServer+WebSocketTransformer, no dependencies, shares models with the client,dart compile exeto a native binary, run under systemd, reverse proxied by nginx on the same VPS. ~200-400 lines with rooms. - Supabase Realtime: fastest prototype; free tier ~200 connections / 100 msg/s.
- Centrifugo: single Go binary, self-hosted pub/sub with a Dart client. Note: Ably and Pusher Flutter packages lack web support; Firebase RTDB has higher latency. No mature Flame multiplayer package exists (2026).
State to send
Reuse the ghost frame: x, y (float32), player state (u8), facing and ability flags; about 10-14 bytes per player per frame. 10 Hz is enough (8 players = ~1 KB/s total). Render remote players 2-3 frames (200-300 ms) behind using spline interpolation; dead-reckon with last velocity + gravity if a packet is late, and lerp corrections over 100-200 ms instead of snapping.
Protocol sketch (binary, 1-byte type prefix)
Client -> server: JOIN_ROOM, LEAVE, READY, FRAME (14-byte ghost frame), FINISH. Server -> client: ROOM_STATE, PLAYER_JOINED, PLAYER_LEFT, COUNTDOWN, GO, PLAYER_FRAME (player id + frame), PLAYER_FINISHED, RACE_RESULTS.
Matchmaking for a small player base
Room codes (4-6 characters, share with friends) first; a room browser and "quick play" are one REST endpoint each. Store rooms in MySQL with a TTL (~10 min). No ELO. Random matchmaking fails below ~100 daily active users.
Risks
WebSockets behind corporate proxies; keeping the game server up (systemd auto-restart, monitoring); mobile backgrounding drops the socket (reconnect, handle "player left"); use the server clock for countdown and finish times, never client clocks.