How artnharsh/brainbrush works
Purpose and Top-Level Entry Points
BrainBrush is a two-process real-time multiplayer drawing/guessing game. The backend exposes REST authentication/history APIs and Socket.IO game transport; the frontend is a React single-page client. Stack startup is coordinated by docker-compose.yml. Backend bootstraps from backend/src/server.ts and application wiring in backend/src/app.ts; frontend mounts from frontend/src/main.tsx through frontend/src/App.tsx.
Backend HTTP, Authentication, and Persistence
Express routes separate OAuth/session lifecycle from player-history reads:
• Google OAuth, JWT handoff, and logout: backend/src/controllers/authController.ts, backend/src/routes/authRoutes.ts, backend/src/config/passport.ts
• JWT request protection and centralized failures: backend/src/middlewares/authMiddleware.ts, backend/src/middlewares/errorMiddleware.ts
• Persistent user identity and completed-match history: backend/src/models/User.ts, backend/src/models/GameHistory.ts, backend/src/controllers/gameController.ts
MongoDB/Mongoose is the durable system of record; connection configuration is in backend/src/config/db.ts.
Real-Time Game Boundary
Socket.IO is the authoritative live-game interface, initialized and composed under backend/src/sockets/index.ts. It divides event ownership by domain:
• Room membership, host actions, and lifecycle: backend/src/sockets/roomSocket.ts, backend/src/services/roomService.ts
• Turn progression, word choice, guesses, and scoring: backend/src/sockets/gameSocket.ts, backend/src/services/gameService.ts, backend/src/services/scoringService.ts
• Canvas strokes, erasing, clearing, and late-join synchronization: backend/src/sockets/drawingSocket.ts
Clients connect using the same configured API base endpoint via frontend/src/socketClient.ts.
Game State and Flow
A player authenticates, enters a lobby, creates or joins a short-code room, and the host starts a match. The active drawer selects a generated word; drawing events and chat guesses flow through Socket.IO room broadcasts. Server-side game services evaluate correct guesses, apply scores, advance turns/rounds, and finish the match. Game-specific contracts and timing/word support reside in backend/src/types/gameTypes.ts, backend/src/utils/timer.ts, and backend/src/utils/wordGenerator.ts.
Redis, configured by backend/src/config/redis.ts, supports the live/distributed state layer, while MongoDB retains users and history after a game.
Frontend Navigation and State Ownership
Route-level screens represent the main user journey:
• Authentication transition and login: frontend/src/pages/LoginPage.tsx, frontend/src/pages/AuthSuccessPage.tsx
• Room discovery/configuration: frontend/src/pages/LobbyPage.tsx
• Active match and historical results: frontend/src/pages/GamePage.tsx, frontend/src/pages/PlayerHistoryPage.tsx
frontend/src/routes.tsx defines navigation, while frontend/src/components/ProtectedRoute.tsx gates authenticated paths. Axios REST traffic is centralized in frontend/src/api/axiosClient.ts.
Interactive Game Client
Zustand state in frontend/src/store/useGameStore.ts is the client-side projection of socket-delivered room/game state. frontend/src/hooks/useSocket.ts binds socket events to that state and UI. GamePage composes drawing, messaging, player/score visibility, and word-selection surfaces from frontend/src/components/CanvasBoard.tsx, frontend/src/components/ChatBox.tsx, frontend/src/components/WordSelectionModal.tsx, and frontend/src/components/ScoreBoard.tsx. Canvas coordinate/rendering helpers are isolated in frontend/src/utils/drawUtils.ts.
Runtime and Deployment Dependencies
Backend runs Node/TypeScript with Express, Socket.IO, Passport Google OAuth, MongoDB, Redis, and JWT configuration from backend/src/config/env.ts. Frontend is built by Vite and served in production through its container/Nginx setup: frontend/Dockerfile, frontend/nginx.conf. Docker Compose connects frontend and backend containers; external MongoDB, Redis, and Google OAuth credentials remain required infrastructure.
Main entry points: routes.tsx Routes and access gate · index.ts Live game gateway · app.ts HTTP API · server.ts Express and Socket.IO server
Components 20
React client
- React application
frontend/src/App.tsx - Routes and access gate
frontend/src/routes.tsx - Login and OAuth return
frontend/src/pages/LoginPage.tsx - Lobby
frontend/src/pages/LobbyPage.tsx - Game screen · Composes canvas, chat, word selection, and scores.
frontend/src/pages/GamePage.tsx - Player history
frontend/src/pages/PlayerHistoryPage.tsx - Socket state projection · Applies live room and game events to client state.
frontend/src/hooks/useSocket.ts - REST client
frontend/src/api/axiosClient.ts - Nginx production frontend
frontend/nginx.conf
Node backend
- Express and Socket.IO server
backend/src/server.ts - HTTP API · Authentication and player-history endpoints.
backend/src/app.ts - OAuth and JWT sessions · Google OAuth handoff and logout lifecycle.
backend/src/controllers/authController.ts - Game history API
backend/src/controllers/gameController.ts - Live game gateway · Authoritative real-time game interface.
backend/src/sockets/index.ts - Room lifecycle · Handles membership, host actions, and room lifecycle.
backend/src/sockets/roomSocket.ts - Match engine · Evaluates guesses, scores, turns, rounds, and completion.
backend/src/sockets/gameSocket.ts - Drawing sync · Broadcasts strokes and synchronizes late joiners.
backend/src/sockets/drawingSocket.ts
Infrastructure
Other components
- Docker Compose · Connects frontend and backend containers.
docker-compose.yml
Connections 26
- Docker Compose → Nginx production frontend: runs
- Docker Compose → Express and Socket.IO server: runs
- Nginx production frontend → React application: serves build
- React application → Routes and access gate: navigates through
- Routes and access gate → Login and OAuth return: auth journey
- Routes and access gate → Lobby: room journey
- Routes and access gate → Game screen: match journey
- Routes and access gate → Player history: history journey
- Login and OAuth return → REST client: OAuth and session API
- Player history → REST client: reads history
- Lobby → Socket state projection: room events
- Game screen → Socket state projection: live state and actions
- REST client → HTTP API: HTTPS REST
- Socket state projection → Live game gateway: Socket.IO
- Express and Socket.IO server → HTTP API: hosts
- Express and Socket.IO server → Live game gateway: hosts
- HTTP API → OAuth and JWT sessions: auth routes
- HTTP API → Game history API: history routes
- OAuth and JWT sessions → MongoDB: user identity
- Game history API → MongoDB: completed matches
- Live game gateway → Room lifecycle: room events
- Live game gateway → Match engine: game events
- Live game gateway → Drawing sync: drawing events
- Room lifecycle → Redis: live rooms
- Match engine → Redis: live match state
- Match engine → MongoDB: persists results