# krishbansal10/chatapp architecture

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

- Interactive diagram: https://gitdiagram.com/krishbansal10/chatapp
- Repository: https://github.com/krishbansal10/chatapp

## Overview

#### Purpose and Runtime
A Next.js collaboration/chat application organized around servers, channels, member conversations, real-time messaging, file attachments, and LiveKit media rooms. It uses the App Router for UI and REST-style mutations, plus the Pages Router API for Socket.IO endpoints. Prisma provides persistence; authentication, upload, and media integrations are indicated by dedicated route/provider modules.

#### Entry Points and Route Domains
- Root composition and global styling begin at `app/layout.tsx` and `app/globals.css`.
- Authenticated application shell: `app/(main)/layout.tsx`; server-scoped shell: `app/(main)/(routes)/servers/[serverId]/layout.tsx`.
- Setup and onboarding: `app/(setup)/page.tsx`.
- Authentication routes: `app/(auth)/layout.tsx`, `app/(auth)/(routes)/sign-in/[[...sign-in]]/page.tsx`.
- Invitation acceptance is isolated under `app/(invite)/(routes)/invite/[inviteCode]/page.tsx`.
- `middleware.ts` is the cross-route request boundary, likely enforcing access/redirect behavior.

#### Server, Channel, and Membership Model
The primary domain hierarchy is server → channels/members → channel messages or direct conversations. Prisma schema is the authoritative persisted model at `prisma/schema.prisma`, accessed through `lib/db.ts`.
- Server landing and scoped navigation: `app/(main)/(routes)/servers/[serverId]/page.tsx`, `components/server/server-sidebar.tsx`.
- Channel chat route: `app/(main)/(routes)/servers/[serverId]/channels/[channelId]/page.tsx`.
- Member-to-member conversation route: `app/(main)/(routes)/servers/[serverId]/conversations/[memberId]/page.tsx`.
- API boundaries manage server lifecycle, membership, invites, and channel changes: `app/api/servers/route.ts`, `app/api/members/[memberId]/route.ts`, `app/api/channels/[channelId]/route.ts`.

#### Identity and Authorization Boundary
Profile resolution is centralized rather than duplicated in route code: `lib/current-profile.ts`, `lib/current-profile-pages.ts`, and `lib/initial-profile.ts`. Auth UI is separated from main UI via route groups and layouts. Server membership and invite-code operations are explicit backend actions in `app/api/servers/[serverId]/invite-code/route.ts` and `app/api/servers/[serverId]/leave/route.ts`.

#### Messaging and Real-Time Delivery
Chat UI composes headers, input, message history, and welcome states in `components/chat/chat-header.tsx`, `components/chat/chat-input.tsx`, and `components/chat/chat-messages.tsx`. Client state is split between paginated/history querying and socket event subscription: `hooks/use-chat-query.ts` and `hooks/use-chat-socket.ts`.
Socket.IO is hosted through Pages API endpoints, separating long-lived transport from App Router HTTP APIs:
- Socket initialization: `pages/api/socket/io.ts`
- Channel-message events: `pages/api/socket/messages/index.ts`
- Direct-message events: `pages/api/socket/direct-messages/index.ts`
HTTP creation for direct messages remains at `app/api/direct-messages/route.ts`.

#### Media and File Boundaries
Live audio/video access is tokenized or brokered by `app/api/livekit/route.ts`, consumed by `components/media-room.tsx` and invoked from `components/chat/chat-video-button.tsx`. UploadThing integration is isolated behind `app/api/uploadthing/core.ts` and `app/api/uploadthing/route.ts`, with client file selection/upload UI in `components/file-upload.tsx`. This keeps external media and storage credentials out of chat/server UI components.

#### Client Providers and Interaction Layer
Application-wide providers establish modal state, query caching, sockets, and theming: `components/providers/modal-provider.tsx`, `components/providers/query-provider.tsx`, `components/providers/socket-provider.tsx`. Server/channel/member actions are primarily expressed through reusable modal workflows under `components/modals/create-server-modal.tsx`, `components/modals/create-channel-modal.tsx`, and `components/modals/members-modal.tsx`. Navigation is server-centric via `components/navigation/navigation-sidebar.tsx` and `components/server/server-sidebar.tsx`.

Main entry points: [`components/chat/chat-messages.tsx`](https://github.com/krishbansal10/chatapp/blob/main/components/chat/chat-messages.tsx) (Chat composition), [`prisma/schema.prisma`](https://github.com/krishbansal10/chatapp/blob/main/prisma/schema.prisma) (Collaboration database), [`app/(main)/(routes)/servers/[serverId]/layout.tsx`](https://github.com/krishbansal10/chatapp/blob/main/app/(main)/(routes)/servers/%5BserverId%5D/layout.tsx) (Server-scoped shell), [`pages/api/socket/io.ts`](https://github.com/krishbansal10/chatapp/blob/main/pages/api/socket/io.ts) (Socket.IO initialization)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Client UI"]
  node_root["Root composition<br/>Next.js layout<br/>[layout.tsx]"]
  node_auth["Auth routes<br/>route group<br/>[layout.tsx]"]
  node_setup["Setup onboarding<br/>page route<br/>[page.tsx]"]
  node_main_shell["Authenticated shell<br/>route layout<br/>[layout.tsx]"]
  node_server_shell["Server-scoped shell<br/>route layout<br/>[layout.tsx]"]
  node_navigation["Server navigation<br/>navigation components"]
  node_conversation_routes["Chat routes<br/>page routes<br/>[page.tsx]"]
  node_chat_ui["Chat composition<br/>chat components<br/>[chat-messages.tsx]"]
  node_client_state["Client interaction state<br/>providers and hooks"]
  node_media_room["Media room<br/>media component<br/>[media-room.tsx]"]
end

subgraph group_app["Next.js App Router"]
  node_middleware{{"Request boundary<br/>Next.js middleware<br/>[middleware.ts]"}}
  node_server_api{{"Server &amp; membership API<br/>App Router API<br/>[route.ts]"}}
  node_direct_message_api{{"Direct message API<br/>App Router API<br/>[route.ts]"}}
end

subgraph group_realtime["Real-time &amp; Integrations"]
  node_socket_init{{"Socket.IO initialization<br/>Pages API endpoint<br/>[io.ts]"}}
  node_channel_events["Channel message events<br/>Socket.IO endpoint<br/>[index.ts]"]
  node_direct_events["Direct message events<br/>Socket.IO endpoint<br/>[index.ts]"]
  node_livekit_api{{"LiveKit broker<br/>App Router API<br/>[route.ts]"}}
  node_upload_api{{"UploadThing boundary<br/>upload API<br/>[route.ts]"}}
end

subgraph group_data["Identity &amp; Data"]
  node_profile["Profile resolution<br/>identity service<br/>[current-profile.ts]"]
  node_prisma[("Collaboration database<br/>Prisma persistence<br/>[schema.prisma]")]
end

node_root -->|"composes"| node_auth
node_root -->|"composes"| node_main_shell
node_middleware -.->|"guards / redirects"| node_auth
node_middleware -.->|"guards access"| node_main_shell
node_auth -->|"establishes identity"| node_profile
node_setup -->|"initializes profile"| node_profile
node_main_shell -->|"renders scoped app"| node_server_shell
node_server_shell -->|"hosts"| node_navigation
node_server_shell -->|"renders"| node_conversation_routes
node_conversation_routes -->|"composes"| node_chat_ui
node_chat_ui -->|"uses queries and subscriptions"| node_client_state
node_client_state -->|"connects"| node_socket_init
node_socket_init -->|"serves"| node_channel_events
node_socket_init -->|"serves"| node_direct_events
node_chat_ui -->|"creates direct messages"| node_direct_message_api
node_server_api -->|"authorizes with"| node_profile
node_server_api -->|"manages domain records"| node_prisma
node_direct_message_api -->|"persists"| node_prisma
node_channel_events -->|"persists and broadcasts"| node_prisma
node_direct_events -->|"persists and broadcasts"| node_prisma
node_chat_ui -->|"requests room access"| node_livekit_api
node_livekit_api -->|"authorizes"| node_media_room
node_chat_ui -->|"uploads attachments"| node_upload_api

click node_root "https://github.com/krishbansal10/chatapp/blob/main/app/layout.tsx"
click node_middleware "https://github.com/krishbansal10/chatapp/blob/main/middleware.ts"
click node_auth "https://github.com/krishbansal10/chatapp/blob/main/app/(auth)/layout.tsx"
click node_setup "https://github.com/krishbansal10/chatapp/blob/main/app/(setup)/page.tsx"
click node_main_shell "https://github.com/krishbansal10/chatapp/blob/main/app/(main)/layout.tsx"
click node_server_shell "https://github.com/krishbansal10/chatapp/blob/main/app/(main)/(routes)/servers/%5BserverId%5D/layout.tsx"
click node_navigation "https://github.com/krishbansal10/chatapp/blob/main/components/navigation/navigation-sidebar.tsx"
click node_conversation_routes "https://github.com/krishbansal10/chatapp/blob/main/app/(main)/(routes)/servers/%5BserverId%5D/channels/%5BchannelId%5D/page.tsx"
click node_chat_ui "https://github.com/krishbansal10/chatapp/blob/main/components/chat/chat-messages.tsx"
click node_client_state "https://github.com/krishbansal10/chatapp/blob/main/components/providers/socket-provider.tsx"
click node_server_api "https://github.com/krishbansal10/chatapp/blob/main/app/api/servers/route.ts"
click node_direct_message_api "https://github.com/krishbansal10/chatapp/blob/main/app/api/direct-messages/route.ts"
click node_socket_init "https://github.com/krishbansal10/chatapp/blob/main/pages/api/socket/io.ts"
click node_channel_events "https://github.com/krishbansal10/chatapp/blob/main/pages/api/socket/messages/index.ts"
click node_direct_events "https://github.com/krishbansal10/chatapp/blob/main/pages/api/socket/direct-messages/index.ts"
click node_livekit_api "https://github.com/krishbansal10/chatapp/blob/main/app/api/livekit/route.ts"
click node_media_room "https://github.com/krishbansal10/chatapp/blob/main/components/media-room.tsx"
click node_upload_api "https://github.com/krishbansal10/chatapp/blob/main/app/api/uploadthing/route.ts"
click node_profile "https://github.com/krishbansal10/chatapp/blob/main/lib/current-profile.ts"
click node_prisma "https://github.com/krishbansal10/chatapp/blob/main/prisma/schema.prisma"

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_root,node_auth,node_setup,node_main_shell,node_server_shell,node_navigation,node_conversation_routes,node_chat_ui,node_client_state,node_media_room toneBlue
class node_middleware,node_server_api,node_direct_message_api toneAmber
class node_socket_init,node_channel_events,node_direct_events,node_livekit_api,node_upload_api toneMint
class node_profile,node_prisma toneRose
```

## Components

### Client UI

App shells, navigation, chat, and interaction state.

- [Root composition](https://github.com/krishbansal10/chatapp/blob/main/app/layout.tsx): Next.js layout · Global layout and styling entry point. · `app/layout.tsx`
- [Auth routes](https://github.com/krishbansal10/chatapp/blob/main/app/(auth)/layout.tsx): route group · Separated authentication UI and sign-in flow. · `app/(auth)/layout.tsx`
- [Setup onboarding](https://github.com/krishbansal10/chatapp/blob/main/app/(setup)/page.tsx): page route · `app/(setup)/page.tsx`
- [Authenticated shell](https://github.com/krishbansal10/chatapp/blob/main/app/(main)/layout.tsx): route layout · Hosts the signed-in application experience. · `app/(main)/layout.tsx`
- [Server-scoped shell](https://github.com/krishbansal10/chatapp/blob/main/app/(main)/(routes)/servers/%5BserverId%5D/layout.tsx): route layout · Scopes navigation and content to a server. · `app/(main)/(routes)/servers/[serverId]/layout.tsx`
- [Server navigation](https://github.com/krishbansal10/chatapp/blob/main/components/navigation/navigation-sidebar.tsx): navigation components · Global server switcher and server sidebar. · `components/navigation/navigation-sidebar.tsx`
- [Chat routes](https://github.com/krishbansal10/chatapp/blob/main/app/(main)/(routes)/servers/%5BserverId%5D/channels/%5BchannelId%5D/page.tsx): page routes · Channel and member conversation views. · `app/(main)/(routes)/servers/[serverId]/channels/[channelId]/page.tsx`
- [Chat composition](https://github.com/krishbansal10/chatapp/blob/main/components/chat/chat-messages.tsx): chat components · Headers, input, history, welcome state, and video launch. · `components/chat/chat-messages.tsx`
- [Client interaction state](https://github.com/krishbansal10/chatapp/blob/main/components/providers/socket-provider.tsx): providers and hooks · Modal workflows, query cache, paginated chat, and socket subscriptions. · `components/providers/socket-provider.tsx`
- [Media room](https://github.com/krishbansal10/chatapp/blob/main/components/media-room.tsx): media component · Consumes LiveKit access for audio/video rooms. · `components/media-room.tsx`

### Next.js App Router

Route rendering and HTTP mutation boundaries.

- [Request boundary](https://github.com/krishbansal10/chatapp/blob/main/middleware.ts): Next.js middleware · Applies cross-route access and redirect behavior. · `middleware.ts`
- [Server & membership API](https://github.com/krishbansal10/chatapp/blob/main/app/api/servers/route.ts): App Router API · Creates servers and manages members, invites, leaving, and channels. · `app/api/servers/route.ts`
- [Direct message API](https://github.com/krishbansal10/chatapp/blob/main/app/api/direct-messages/route.ts): App Router API · HTTP creation boundary for direct messages. · `app/api/direct-messages/route.ts`

### Real-time & Integrations

Socket transport and external media/upload boundaries.

- [Socket.IO initialization](https://github.com/krishbansal10/chatapp/blob/main/pages/api/socket/io.ts): Pages API endpoint · Hosts the long-lived Socket.IO transport. · `pages/api/socket/io.ts`
- [Channel message events](https://github.com/krishbansal10/chatapp/blob/main/pages/api/socket/messages/index.ts): Socket.IO endpoint · `pages/api/socket/messages/index.ts`
- [Direct message events](https://github.com/krishbansal10/chatapp/blob/main/pages/api/socket/direct-messages/index.ts): Socket.IO endpoint · `pages/api/socket/direct-messages/index.ts`
- [LiveKit broker](https://github.com/krishbansal10/chatapp/blob/main/app/api/livekit/route.ts): App Router API · Issues or brokers access to media rooms. · `app/api/livekit/route.ts`
- [UploadThing boundary](https://github.com/krishbansal10/chatapp/blob/main/app/api/uploadthing/route.ts): upload API · Isolates file-storage integration and credentials. · `app/api/uploadthing/route.ts`

### Identity & Data

Shared identity resolution and persisted collaboration data.

- [Profile resolution](https://github.com/krishbansal10/chatapp/blob/main/lib/current-profile.ts): identity service · Centralized current and initial profile resolution. · `lib/current-profile.ts`
- [Collaboration database](https://github.com/krishbansal10/chatapp/blob/main/prisma/schema.prisma): Prisma persistence · Authoritative server, channel, member, and message model. · `prisma/schema.prisma`

## Connections

- Root composition → Auth routes: composes
- Root composition → Authenticated shell: composes
- Request boundary → Auth routes: guards / redirects
- Request boundary → Authenticated shell: guards access
- Auth routes → Profile resolution: establishes identity
- Setup onboarding → Profile resolution: initializes profile
- Authenticated shell → Server-scoped shell: renders scoped app
- Server-scoped shell → Server navigation: hosts
- Server-scoped shell → Chat routes: renders
- Chat routes → Chat composition: composes
- Chat composition → Client interaction state: uses queries and subscriptions
- Client interaction state → Socket.IO initialization: connects
- Socket.IO initialization → Channel message events: serves
- Socket.IO initialization → Direct message events: serves
- Chat composition → Direct message API: creates direct messages
- Server & membership API → Profile resolution: authorizes with
- Server & membership API → Collaboration database: manages domain records
- Direct message API → Collaboration database: persists
- Channel message events → Collaboration database: persists and broadcasts
- Direct message events → Collaboration database: persists and broadcasts
- Chat composition → LiveKit broker: requests room access
- LiveKit broker → Media room: authorizes
- Chat composition → UploadThing boundary: uploads attachments
