# artnharsh/brainbrush architecture

> Architecture diagram of the GitHub repository artnharsh/brainbrush, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/artnharsh/brainbrush
- Repository: https://github.com/artnharsh/brainbrush

## Overview

#### 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: [`frontend/src/routes.tsx`](https://github.com/artnharsh/brainbrush/blob/main/frontend/src/routes.tsx) (Routes and access gate), [`backend/src/sockets/index.ts`](https://github.com/artnharsh/brainbrush/blob/main/backend/src/sockets/index.ts) (Live game gateway), [`backend/src/app.ts`](https://github.com/artnharsh/brainbrush/blob/main/backend/src/app.ts) (HTTP API), [`backend/src/server.ts`](https://github.com/artnharsh/brainbrush/blob/main/backend/src/server.ts) (Express and Socket.IO server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React client"]
  node_frontend_app["React application<br/>SPA entry<br/>[App.tsx]"]
  node_frontend_routes["Routes and access gate<br/>client routing<br/>[routes.tsx]"]
  node_frontend_auth["Login and OAuth return<br/>auth screens<br/>[LoginPage.tsx]"]
  node_frontend_lobby["Lobby<br/>room screen<br/>[LobbyPage.tsx]"]
  node_frontend_game["Game screen<br/>match UI<br/>[GamePage.tsx]"]
  node_frontend_history["Player history<br/>history screen"]
  node_socket_state["Socket state projection<br/>Zustand and socket hook<br/>[useSocket.ts]"]
  node_rest_client["REST client<br/>Axios client<br/>[axiosClient.ts]"]
  node_nginx["Nginx production frontend<br/>static web server<br/>[nginx.conf]"]
end

subgraph group_backend["Node backend"]
  node_backend_server["Express and Socket.IO server<br/>backend entry<br/>[server.ts]"]
  node_http_api["HTTP API<br/>Express routes<br/>[app.ts]"]
  node_auth["OAuth and JWT sessions<br/>authentication<br/>[authController.ts]"]
  node_game_history_api["Game history API<br/>history controller<br/>[gameController.ts]"]
  node_socket_gateway["Live game gateway<br/>Socket.IO composition<br/>[index.ts]"]
  node_room_domain["Room lifecycle<br/>room socket and service<br/>[roomSocket.ts]"]
  node_game_domain["Match engine<br/>game and scoring services<br/>[gameSocket.ts]"]
  node_drawing_domain["Drawing sync<br/>drawing socket<br/>[drawingSocket.ts]"]
end

subgraph group_data["Infrastructure"]
  node_mongodb[("MongoDB<br/>durable datastore<br/>[db.ts]")]
  node_redis[("Redis<br/>live state store<br/>[redis.ts]")]
end

node_compose{{"Docker Compose<br/>runtime orchestration<br/>[docker-compose.yml]"}}

node_compose -->|"runs"| node_nginx
node_compose -->|"runs"| node_backend_server
node_nginx -->|"serves build"| node_frontend_app
node_frontend_app -->|"navigates through"| node_frontend_routes
node_frontend_routes -->|"auth journey"| node_frontend_auth
node_frontend_routes -->|"room journey"| node_frontend_lobby
node_frontend_routes -->|"match journey"| node_frontend_game
node_frontend_routes -->|"history journey"| node_frontend_history
node_frontend_auth -->|"OAuth and session API"| node_rest_client
node_frontend_history -->|"reads history"| node_rest_client
node_frontend_lobby -->|"room events"| node_socket_state
node_frontend_game -->|"live state and actions"| node_socket_state
node_rest_client -->|"HTTPS REST"| node_http_api
node_socket_state -->|"Socket.IO"| node_socket_gateway
node_backend_server -->|"hosts"| node_http_api
node_backend_server -->|"hosts"| node_socket_gateway
node_http_api -->|"auth routes"| node_auth
node_http_api -->|"history routes"| node_game_history_api
node_auth -->|"user identity"| node_mongodb
node_game_history_api -->|"completed matches"| node_mongodb
node_socket_gateway -->|"room events"| node_room_domain
node_socket_gateway -->|"game events"| node_game_domain
node_socket_gateway -->|"drawing events"| node_drawing_domain
node_room_domain -->|"live rooms"| node_redis
node_game_domain -->|"live match state"| node_redis
node_game_domain -->|"persists results"| node_mongodb

click node_compose "https://github.com/artnharsh/brainbrush/blob/main/docker-compose.yml"
click node_frontend_app "https://github.com/artnharsh/brainbrush/blob/main/frontend/src/App.tsx"
click node_frontend_routes "https://github.com/artnharsh/brainbrush/blob/main/frontend/src/routes.tsx"
click node_frontend_auth "https://github.com/artnharsh/brainbrush/blob/main/frontend/src/pages/LoginPage.tsx"
click node_frontend_lobby "https://github.com/artnharsh/brainbrush/blob/main/frontend/src/pages/LobbyPage.tsx"
click node_frontend_game "https://github.com/artnharsh/brainbrush/blob/main/frontend/src/pages/GamePage.tsx"
click node_frontend_history "https://github.com/artnharsh/brainbrush/blob/main/frontend/src/pages/PlayerHistoryPage.tsx"
click node_socket_state "https://github.com/artnharsh/brainbrush/blob/main/frontend/src/hooks/useSocket.ts"
click node_rest_client "https://github.com/artnharsh/brainbrush/blob/main/frontend/src/api/axiosClient.ts"
click node_backend_server "https://github.com/artnharsh/brainbrush/blob/main/backend/src/server.ts"
click node_http_api "https://github.com/artnharsh/brainbrush/blob/main/backend/src/app.ts"
click node_auth "https://github.com/artnharsh/brainbrush/blob/main/backend/src/controllers/authController.ts"
click node_game_history_api "https://github.com/artnharsh/brainbrush/blob/main/backend/src/controllers/gameController.ts"
click node_socket_gateway "https://github.com/artnharsh/brainbrush/blob/main/backend/src/sockets/index.ts"
click node_room_domain "https://github.com/artnharsh/brainbrush/blob/main/backend/src/sockets/roomSocket.ts"
click node_game_domain "https://github.com/artnharsh/brainbrush/blob/main/backend/src/sockets/gameSocket.ts"
click node_drawing_domain "https://github.com/artnharsh/brainbrush/blob/main/backend/src/sockets/drawingSocket.ts"
click node_mongodb "https://github.com/artnharsh/brainbrush/blob/main/backend/src/config/db.ts"
click node_redis "https://github.com/artnharsh/brainbrush/blob/main/backend/src/config/redis.ts"
click node_nginx "https://github.com/artnharsh/brainbrush/blob/main/frontend/nginx.conf"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_routes,node_frontend_auth,node_frontend_lobby,node_frontend_game,node_frontend_history,node_socket_state,node_rest_client,node_nginx toneBlue
class node_backend_server,node_http_api,node_auth,node_game_history_api,node_socket_gateway,node_room_domain,node_game_domain,node_drawing_domain toneAmber
class node_mongodb,node_redis toneMint
class node_compose toneNeutral
```

## Components

### React client

- [React application](https://github.com/artnharsh/brainbrush/blob/main/frontend/src/App.tsx): SPA entry · `frontend/src/App.tsx`
- [Routes and access gate](https://github.com/artnharsh/brainbrush/blob/main/frontend/src/routes.tsx): client routing · `frontend/src/routes.tsx`
- [Login and OAuth return](https://github.com/artnharsh/brainbrush/blob/main/frontend/src/pages/LoginPage.tsx): auth screens · `frontend/src/pages/LoginPage.tsx`
- [Lobby](https://github.com/artnharsh/brainbrush/blob/main/frontend/src/pages/LobbyPage.tsx): room screen · `frontend/src/pages/LobbyPage.tsx`
- [Game screen](https://github.com/artnharsh/brainbrush/blob/main/frontend/src/pages/GamePage.tsx): match UI · Composes canvas, chat, word selection, and scores. · `frontend/src/pages/GamePage.tsx`
- [Player history](https://github.com/artnharsh/brainbrush/blob/main/frontend/src/pages/PlayerHistoryPage.tsx): history screen · `frontend/src/pages/PlayerHistoryPage.tsx`
- [Socket state projection](https://github.com/artnharsh/brainbrush/blob/main/frontend/src/hooks/useSocket.ts): Zustand and socket hook · Applies live room and game events to client state. · `frontend/src/hooks/useSocket.ts`
- [REST client](https://github.com/artnharsh/brainbrush/blob/main/frontend/src/api/axiosClient.ts): Axios client · `frontend/src/api/axiosClient.ts`
- [Nginx production frontend](https://github.com/artnharsh/brainbrush/blob/main/frontend/nginx.conf): static web server · `frontend/nginx.conf`

### Node backend

- [Express and Socket.IO server](https://github.com/artnharsh/brainbrush/blob/main/backend/src/server.ts): backend entry · `backend/src/server.ts`
- [HTTP API](https://github.com/artnharsh/brainbrush/blob/main/backend/src/app.ts): Express routes · Authentication and player-history endpoints. · `backend/src/app.ts`
- [OAuth and JWT sessions](https://github.com/artnharsh/brainbrush/blob/main/backend/src/controllers/authController.ts): authentication · Google OAuth handoff and logout lifecycle. · `backend/src/controllers/authController.ts`
- [Game history API](https://github.com/artnharsh/brainbrush/blob/main/backend/src/controllers/gameController.ts): history controller · `backend/src/controllers/gameController.ts`
- [Live game gateway](https://github.com/artnharsh/brainbrush/blob/main/backend/src/sockets/index.ts): Socket.IO composition · Authoritative real-time game interface. · `backend/src/sockets/index.ts`
- [Room lifecycle](https://github.com/artnharsh/brainbrush/blob/main/backend/src/sockets/roomSocket.ts): room socket and service · Handles membership, host actions, and room lifecycle. · `backend/src/sockets/roomSocket.ts`
- [Match engine](https://github.com/artnharsh/brainbrush/blob/main/backend/src/sockets/gameSocket.ts): game and scoring services · Evaluates guesses, scores, turns, rounds, and completion. · `backend/src/sockets/gameSocket.ts`
- [Drawing sync](https://github.com/artnharsh/brainbrush/blob/main/backend/src/sockets/drawingSocket.ts): drawing socket · Broadcasts strokes and synchronizes late joiners. · `backend/src/sockets/drawingSocket.ts`

### Infrastructure

- [MongoDB](https://github.com/artnharsh/brainbrush/blob/main/backend/src/config/db.ts): durable datastore · Stores user identity and completed-match history. · `backend/src/config/db.ts`
- [Redis](https://github.com/artnharsh/brainbrush/blob/main/backend/src/config/redis.ts): live state store · Supports distributed live game state. · `backend/src/config/redis.ts`

### Other components

- [Docker Compose](https://github.com/artnharsh/brainbrush/blob/main/docker-compose.yml): runtime orchestration · Connects frontend and backend containers. · `docker-compose.yml`

## Connections

- 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
