# kartikeya-guthub/collabhub architecture

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

- Interactive diagram: https://gitdiagram.com/kartikeya-guthub/collabhub
- Repository: https://github.com/kartikeya-guthub/collabhub

## Overview

#### Purpose and entry points
CollabHub is a room-based collaborative workspace combining live code editing, a shared tldraw whiteboard, presence, code execution, and AI assistance. The browser application is a Next.js client rooted at `client/src/app/layout.tsx` and `client/src/app/page.tsx`; room collaboration is entered through `client/src/app/room/[id]/page.tsx` and its join route. The backend process starts from `server/src/index.ts`.

#### Identity, rooms, and access boundary
Postgres stores users, rooms, and memberships, established through migrations in `server/migrations/1783655038830_create-users-table.ts`, `server/migrations/1783655079156_create-rooms-table.ts`, and `server/migrations/1783655093393_create-room-members-table.ts`. Authentication endpoints issue JWT-backed identity via `server/src/routes/auth.ts`; protected HTTP and realtime operations use `server/src/middleware/requireAuth.ts`. Room creation, membership, and room lookup flow through `server/src/routes/rooms.ts`.

#### Client workspace composition
The room UI composes independent collaborative surfaces and auxiliary panels: Monaco/Yjs code editing in `client/src/components/CodeEditor.tsx`, tldraw canvas integration in `client/src/components/Whiteboard.tsx`, and merged participant state in `client/src/components/UnifiedPresenceList.tsx`. The dashboard (`client/src/app/dashboard/page.tsx`) is the room-discovery/management boundary, while room pages own the active session experience.

#### Code synchronization and presence
Code collaboration uses Yjs over Socket.IO. Client connection/provider setup is separated into `client/src/lib/socket.ts` and `client/src/lib/yjsProvider.ts`; the server-side Socket.IO coordination is in `server/src/sockets/index.ts`. Redis, configured through `server/src/redis.ts`, supports code-state rehydration and horizontal scaling. The client merges code and whiteboard presence through `client/src/lib/presence.ts`, rather than requiring the two sync engines to share a protocol.

#### Whiteboard synchronization
Whiteboard state runs through a separate raw-WebSocket `@tldraw/sync` service implemented at `server/src/tldrawSync/index.ts` and consumed by `client/src/components/Whiteboard.tsx`. It shares the JWT/room authorization model but not Yjs transport or storage. This is the principal scaling boundary: the code path is Redis-capable across instances, while tldraw sync is currently intended as a single instance.

#### Execution and AI services
Code submitted from `client/src/components/RunTestsPanel.tsx` reaches `server/src/routes/execute.ts`, which uses Judge0-compatible language mapping in `server/src/judge0/languages.ts` and supports the documented Piston development fallback. Results are broadcast to room participants through the realtime layer. AI requests originate in `client/src/components/AskAIPanel.tsx` and are handled by `server/src/routes/ai.ts` using provider selection/client logic in `server/src/ai/client.ts`; Groq supports fast answers and NVIDIA Nemotron supports deeper streamed responses. `client/src/components/DiagramGenerator.tsx` applies AI-produced diagram intent to the shared whiteboard.

#### Runtime and infrastructure
The system consists of a Next.js/TypeScript frontend and Node/TypeScript backend, with Postgres, Redis, and Docker-managed infrastructure declared in `docker-compose.yml`. The backend integrates external Judge0/RapidAPI and AI provider APIs. A separate local Piston deployment is available under `piston-deployment/docker-compose.yml`, including its reverse-proxy template.

Main entry points: [`server/src/middleware/requireAuth.ts`](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/middleware/requireAuth.ts) (JWT access guard), [`client/src/app/room/[id]/page.tsx`](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/app/room/%5Bid%5D/page.tsx) (Room session), [`server/src/sockets/index.ts`](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/sockets/index.ts) (Code realtime server), [`client/src/lib/yjsProvider.ts`](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/lib/yjsProvider.ts) (Yjs socket provider)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser client"]
  node_app["Next.js app<br/>[layout.tsx]"]
  node_dashboard["Room dashboard<br/>page<br/>[page.tsx]"]
  node_room["Room session<br/>page<br/>[page.tsx]"]
  node_code_editor["Code editor<br/>Monaco + Yjs<br/>[CodeEditor.tsx]"]
  node_yjs_client["Yjs socket provider<br/>realtime client<br/>[yjsProvider.ts]"]
  node_whiteboard["Shared whiteboard<br/>tldraw client<br/>[Whiteboard.tsx]"]
  node_presence["Unified presence<br/>presence merge<br/>[presence.ts]"]
  node_run_tests["Run tests panel<br/>execution client<br/>[RunTestsPanel.tsx]"]
  node_ask_ai["AI panels<br/>AI client<br/>[AskAIPanel.tsx]"]
end

subgraph group_backend["Backend services"]
  node_api["HTTP API<br/>Node routes<br/>[index.ts]"]
  node_auth_guard["JWT access guard<br/>middleware<br/>[requireAuth.ts]"]
  node_auth["Auth routes<br/>identity API<br/>[auth.ts]"]
  node_rooms_api["Room routes<br/>room API<br/>[rooms.ts]"]
  node_socket_server["Code realtime server<br/>Socket.IO + Yjs<br/>[index.ts]"]
  node_tldraw_sync["Whiteboard sync server<br/>raw WebSocket<br/>[index.ts]"]
  node_execute["Execution route<br/>execution API<br/>[execute.ts]"]
  node_ai["AI route and client<br/>AI gateway<br/>[client.ts]"]
end

subgraph group_data["State and infrastructure"]
  node_postgres[("Postgres<br/>database")]
  node_redis[("Redis<br/>distributed state<br/>[redis.ts]")]
  node_judge0{{"Judge0 / Piston<br/>execution provider<br/>[docker-compose.yml]"}}
  node_ai_providers{{"AI providers<br/>external APIs"}}
end

node_app -->|"hosts"| node_dashboard
node_app -->|"hosts"| node_room
node_dashboard -->|"room management"| node_api
node_room -->|"composes"| node_code_editor
node_room -->|"composes"| node_whiteboard
node_room -->|"shows"| node_presence
node_code_editor -->|"syncs through"| node_yjs_client
node_yjs_client -->|"Socket.IO / Yjs"| node_socket_server
node_whiteboard -->|"raw WebSocket"| node_tldraw_sync
node_yjs_client -->|"code presence"| node_presence
node_whiteboard -->|"canvas presence"| node_presence
node_run_tests -->|"submits code"| node_execute
node_ask_ai -->|"requests assistance"| node_ai
node_api -->|"protects requests"| node_auth_guard
node_auth_guard -->|"identity flow"| node_auth
node_auth_guard -->|"authorizes rooms"| node_rooms_api
node_auth_guard -->|"authorizes realtime"| node_socket_server
node_auth_guard -->|"authorizes rooms"| node_tldraw_sync
node_auth -->|"users"| node_postgres
node_rooms_api -->|"rooms and members"| node_postgres
node_socket_server -->|"rehydrates and scales"| node_redis
node_execute -->|"runs submissions"| node_judge0
node_execute -->|"broadcasts results"| node_socket_server
node_ai -->|"selects provider"| node_ai_providers

click node_app "https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/app/layout.tsx"
click node_dashboard "https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/app/dashboard/page.tsx"
click node_room "https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/app/room/%5Bid%5D/page.tsx"
click node_code_editor "https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/components/CodeEditor.tsx"
click node_yjs_client "https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/lib/yjsProvider.ts"
click node_whiteboard "https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/components/Whiteboard.tsx"
click node_presence "https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/lib/presence.ts"
click node_run_tests "https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/components/RunTestsPanel.tsx"
click node_ask_ai "https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/components/AskAIPanel.tsx"
click node_api "https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/index.ts"
click node_auth_guard "https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/middleware/requireAuth.ts"
click node_auth "https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/routes/auth.ts"
click node_rooms_api "https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/routes/rooms.ts"
click node_socket_server "https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/sockets/index.ts"
click node_tldraw_sync "https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/tldrawSync/index.ts"
click node_execute "https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/routes/execute.ts"
click node_ai "https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/ai/client.ts"
click node_postgres "https://github.com/kartikeya-guthub/collabhub/blob/main/server/migrations/1783655038830_create-users-table.ts"
click node_redis "https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/redis.ts"
click node_judge0 "https://github.com/kartikeya-guthub/collabhub/blob/main/piston-deployment/docker-compose.yml"

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_app,node_dashboard,node_room,node_code_editor,node_yjs_client,node_whiteboard,node_presence,node_run_tests,node_ask_ai toneBlue
class node_api,node_auth_guard,node_auth,node_rooms_api,node_socket_server,node_tldraw_sync,node_execute,node_ai toneAmber
class node_postgres,node_redis,node_judge0,node_ai_providers toneMint
```

## Components

### Browser client

Next.js collaborative workspace.

- [Next.js app](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/app/layout.tsx): Next.js · Application shell and client entry points. · `client/src/app/layout.tsx`
- [Room dashboard](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/app/dashboard/page.tsx): page · Room discovery and management boundary. · `client/src/app/dashboard/page.tsx`
- [Room session](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/app/room/%5Bid%5D/page.tsx): page · Active collaborative workspace. · `client/src/app/room/[id]/page.tsx`
- [Code editor](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/components/CodeEditor.tsx): Monaco + Yjs · Collaborative code surface. · `client/src/components/CodeEditor.tsx`
- [Yjs socket provider](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/lib/yjsProvider.ts): realtime client · Creates Socket.IO-backed document synchronization. · `client/src/lib/yjsProvider.ts`
- [Shared whiteboard](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/components/Whiteboard.tsx): tldraw client · Canvas surface using its own sync transport. · `client/src/components/Whiteboard.tsx`
- [Unified presence](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/lib/presence.ts): presence merge · Combines code and whiteboard participant state. · `client/src/lib/presence.ts`
- [Run tests panel](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/components/RunTestsPanel.tsx): execution client · `client/src/components/RunTestsPanel.tsx`
- [AI panels](https://github.com/kartikeya-guthub/collabhub/blob/main/client/src/components/AskAIPanel.tsx): AI client · Requests assistance and applies diagram intent. · `client/src/components/AskAIPanel.tsx`

### Backend services

Node API and realtime coordination.

- [HTTP API](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/index.ts): Node routes · Authenticated identity, room, execution, and AI endpoints. · `server/src/index.ts`
- [JWT access guard](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/middleware/requireAuth.ts): middleware · Protects HTTP and realtime room operations. · `server/src/middleware/requireAuth.ts`
- [Auth routes](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/routes/auth.ts): identity API · Issues JWT-backed identity. · `server/src/routes/auth.ts`
- [Room routes](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/routes/rooms.ts): room API · Creates rooms and manages membership. · `server/src/routes/rooms.ts`
- [Code realtime server](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/sockets/index.ts): Socket.IO + Yjs · Coordinates code synchronization and room broadcasts. · `server/src/sockets/index.ts`
- [Whiteboard sync server](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/tldrawSync/index.ts): raw WebSocket · Single-instance @tldraw/sync service. · `server/src/tldrawSync/index.ts`
- [Execution route](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/routes/execute.ts): execution API · Maps languages and submits code execution. · `server/src/routes/execute.ts`
- [AI route and client](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/ai/client.ts): AI gateway · Selects Groq or NVIDIA Nemotron providers. · `server/src/ai/client.ts`

### State and infrastructure

Persistent, distributed, and external execution services.

- [Postgres](https://github.com/kartikeya-guthub/collabhub/blob/main/server/migrations/1783655038830_create-users-table.ts): database · Users, rooms, and memberships. · `server/migrations/1783655038830_create-users-table.ts`
- [Redis](https://github.com/kartikeya-guthub/collabhub/blob/main/server/src/redis.ts): distributed state · Code-state rehydration and multi-instance scaling. · `server/src/redis.ts`
- [Judge0 / Piston](https://github.com/kartikeya-guthub/collabhub/blob/main/piston-deployment/docker-compose.yml): execution provider · External Judge0-compatible service with local Piston fallback. · `piston-deployment/docker-compose.yml`
- **AI providers**: external APIs · Groq fast answers and NVIDIA Nemotron streaming.

## Connections

- Next.js app → Room dashboard: hosts
- Next.js app → Room session: hosts
- Room dashboard → HTTP API: room management
- Room session → Code editor: composes
- Room session → Shared whiteboard: composes
- Room session → Unified presence: shows
- Code editor → Yjs socket provider: syncs through
- Yjs socket provider → Code realtime server: Socket.IO / Yjs
- Shared whiteboard → Whiteboard sync server: raw WebSocket
- Yjs socket provider → Unified presence: code presence
- Shared whiteboard → Unified presence: canvas presence
- Run tests panel → Execution route: submits code
- AI panels → AI route and client: requests assistance
- HTTP API → JWT access guard: protects requests
- JWT access guard → Auth routes: identity flow
- JWT access guard → Room routes: authorizes rooms
- JWT access guard → Code realtime server: authorizes realtime
- JWT access guard → Whiteboard sync server: authorizes rooms
- Auth routes → Postgres: users
- Room routes → Postgres: rooms and members
- Code realtime server → Redis: rehydrates and scales
- Execution route → Judge0 / Piston: runs submissions
- Execution route → Code realtime server: broadcasts results
- AI route and client → AI providers: selects provider
