#nestjs #react #websockets | Igor C.

The hardest bug in my real-time chess app had nothing to do with chess. 🧠 It was state living in two places at once: the client and the socket. Mega Chess is Nest.js + React with matchmaking, tournaments and live brackets over Socket.IO. chess.js handled the rules. Keeping everyone in sync was the real work. What broke: ➡️ socket.off('match_found') without a handler reference removes every listener for that event, including the ones other pages registered. Players stopped being redirected to their match. ➡️ match_found arrived before my game store knew my color. Both players saw the board from white's side and every move was rejected. ➡️ My tournament bracket never moved past round 1. The loop started from the round that was already finished. ❓ Rules I follow now: 1️⃣ The server owns the clock and the result. "3+2" is 180s plus 2s per move, and the client only renders it. 2️⃣ Named listeners only. 3️⃣ Set the state first, navigate second. 😕 Not perfect: ❗ GameModule and TournamentsModule depend on each other. forwardRef fixed it, but it's a design smell I'd rather untangle. ❗ TypeORM synchronize is fine in dev. Production needs explicit migrations. 👏 Chess rules are a solved problem. Shared state across clients is not. Question for you: in real-time apps, do you keep the server as the single source of truth and treat clients as dumb renderers, or go optimistic and reconcile later? 👇 #NestJS #React #WebSockets

favicon linkedin.com