How kirillpitomets/infinite-chat works
1. Top-level shape
- This is a Next.js App Router application.
- src/app defines the route tree, with a public root page and an authenticated account area under src/app/account.
- The account area includes a chat product surface plus sibling sections for events, meetings, and music, all wrapped by src/app/account/layout.tsx.
2. Primary product subsystem: chat
- The largest subsystem is src/features/chat, which contains the chat UI, message composer, inbox, headers, typing banner, and chat room pages.
- The feature is split by responsibility: chat, message, realtime, ui, pages, and hooks.
- This is the main end-user workflow: inbox selection → chat room → message list/input → live updates.
3. UI composition boundary
- src/shared/components provides reusable building blocks used across the app: sidebar, dialogs, loaders, upload button, search input, typing indicator, preview file UI, notifications, and icon set.
- The chat feature composes these shared primitives rather than owning all visual elements itself.
- src/shared/providers.tsx and context files suggest the app is wrapped in global providers for user, presence, and notifications.
4. Server/domain layer
- src/server is the backend domain layer, organized by business capability:
- chat
- message
- user
- presence
- fileStorage
- Each domain has DTO mappers and typed controller contracts, which indicates a deliberate separation between persistence models and API payloads.
- src/server/index.ts and src/server/api/index.ts likely compose these domain services into the app’s typed server API surface.
5. API boundary and routing
- The repository uses a catch-all API route at src/app/api/[[...slugs]]/route.ts, which acts as a single HTTP entry point for the typed server API.
- Dedicated routes also exist for:
- src/app/api/realtime/route.ts for realtime transport
- src/app/api/uploadthing/route.ts and core.ts for file upload integration
- This means client code talks to a small number of Next.js route handlers, while the real domain logic lives in src/server.
6. Persistence layer
- Data persistence is handled by Prisma:
- prisma/schema.prisma
- many migration files showing an evolving chat schema
- The schema history indicates core tables for users, chats, messages, attachments, and chat-user relations.
- Notable evolution includes:
- user auth IDs and avatar URLs
- chat image URLs
- message soft-delete support
- attachment modeling and type cleanup
- read-state defaults on the chat-user join model
7. Realtime architecture
- Realtime is a first-class subsystem:
- client hooks: useRealtimeChat, useRealtimeInbox, useRealtimeTyping
- server: src/server/api/presence/presence.service.ts
- client transport helpers: src/shared/lib/realtime-client.ts, src/shared/lib/realtime.ts
- The presence layer uses Redis via src/shared/lib/redis.ts, which suggests ephemeral state such as online users and typing indicators is not stored in the main relational database.
- The realtime flow appears to power inbox refreshes, live message updates, typing status, and user presence.
8. Messaging and attachment flow
- Messaging is handled by src/server/api/message plus message-specific hooks in src/features/chat/message/api.
- Attachments are modeled separately and transformed through DTO helpers such as toMessageAttachments.ts.
- File upload goes through UploadThing and then into the message model, so the data flow is: user selects files → upload service stores them → message payload references attachment metadata → UI renders attachments via shared preview components.
9. Client state and interaction model
- The app relies on a mix of React hooks and context for cross-cutting UI state:
- CurrentUserContext
- UserPresenceContext
- NotificationManager
- Feature hooks handle common interaction concerns such as typing indicators, debouncing, file previews, auth redirects, and message context menus.
- This suggests a mostly client-driven chat experience with server mutations and realtime synchronization.
10. Shared contract and validation layer
- src/shared/schemes contains schema definitions for chat, chat preview, message, and user data.
- src/shared/lib/eden.ts and the DTO/type files indicate the frontend and backend share typed contracts rather than relying on loose JSON shapes.
- src/shared/types and src/shared/utils contain cross-feature utilities used to keep the app consistent across routes and domains.
11. Request/user boundary
- src/server/middlewares/userContextMiddleware.ts and src/proxy.ts indicate request-level user context and route protection.
- src/shared/hooks/useAuthRedirect.ts suggests the UI redirects unauthenticated users away from protected account routes.
- In diagram terms, there is a clear boundary between public pages and authenticated account-only functionality.
12. How the main data flows connect
- Chat browsing flow: app route → chat page → inbox query → chat details/messages from server API → rendered through chat feature UI.
- Send message flow: composer UI → message mutation hook → server message service → Prisma write → realtime broadcast/presence update.
- Attachment flow: upload UI → UploadThing route/core → stored file metadata → message/attachment record → attachment rendering in message list.
- Presence flow: client realtime hooks ↔ realtime route/service ↔ Redis-backed presence state ↔ UI indicators.
13. Overall architectural style
- The repo follows a feature-sliced frontend over a domain-organized server inside a single Next.js codebase.
- It is not just a UI app; it combines routing, server domain logic, database persistence, realtime signaling, and file upload handling.
- For an architecture diagram, the most important boundaries are: App Router shell, chat feature UI, typed server API, Prisma database, Redis-backed realtime/presence, and UploadThing-based file storage.
Main entry points: server Server API · chat Chat feature · lib Client libs · schema.prisma Prisma DB
Components 21
Next app
App Router routes, account shell, and API entrypoints.
- App Router · Top-level routing for public and authenticated areas.
src/app - Account area · Authenticated section containing chat plus sibling product pages.
src/app/account - Account layout · Shared wrapper for account subroutes.
src/app/account/layout.tsx - API routes · Catch-all typed API plus realtime and upload endpoints.
src/app/api - Chat routes · Chat entry and room pages under the account area.
src/app/account/chat
Chat feature
Primary user workflow for inbox, rooms, messaging, and realtime updates.
- Chat feature · End-user chat experience composed from inbox, room, message, and realtime pieces.
src/features/chat - Chat UI · Layout, inbox, header, message list, input, typing banner, and page states.
src/features/chat/ui - Message flow · Message querying, sending, editing, deleting, status updates, and attachment shaping.
src/features/chat/message - Realtime hooks · Inbox, chat, and typing subscriptions for live updates.
src/features/chat/realtime - Chat pages · Room and empty-state screens for the chat surface.
src/features/chat/pages
Server domain
Typed backend services and domain contracts.
- Server API · Composition root for domain services and API surface.
src/server - Chat service · Chat domain logic and DTO mapping.
src/server/api/chat - Message service · Message domain logic and DTO mapping.
src/server/api/message - Presence service · Presence and typing state backed by ephemeral storage.
src/server/api/presence - User service · User domain and controller contracts.
src/server/api/user - File storage · Attachment DTO transformation and upload integration support.
src/server/api/fileStorage
Shared layer
Reusable UI, hooks, contexts, contracts, and client libraries.
- Shared UI · Sidebar, dialogs, loaders, inputs, typing indicator, previews, and icon set.
src/shared/components - Contexts · Current user, presence, and notifications providers.
src/shared/context - Client libs · Eden client, realtime helpers, Redis access, and auth/navigation hooks.
src/shared/lib - Shared contracts · Frontend/backend schemas and shared types for chat, message, user, and preview data.
src/shared/schemes
Data/integration
Persistence and external service boundaries.
- Prisma DB · Schema and migrations for users, chats, messages, attachments, and read state.
prisma/schema.prisma
Connections 28
- App Router → Account area: routes · App Router exposes authenticated account section.
- App Router → API routes: mounts · App Router exposes HTTP entrypoints.
- Account area → Account layout: wraps · Shared layout applies across account pages.
- Account area → Chat routes: contains · Account area includes chat route tree.
- Chat routes → Chat feature: renders · Chat routes are implemented by the chat feature.
- Chat feature → Chat UI: composes · Primary UI is assembled from chat-specific views.
- Chat feature → Message flow: orchestrates · Feature coordinates message actions and rendering.
- Chat feature → Realtime hooks: subscribes · Feature consumes live inbox/chat/typing updates.
- Chat feature → Chat pages: uses · Feature provides empty and room page states.
- Chat UI → Shared UI: reuses · Chat UI builds on shared primitives.
- Chat UI → Contexts: reads · UI consumes user, presence, and notification context.
- Message flow → Shared contracts: validates · Message models align with shared schemas.
- Message flow → Client libs: calls · Message hooks use client-side API and helper libraries.
- Realtime hooks → Client libs: connects · Realtime hooks use shared transport helpers.
- API routes → Server API: dispatches · Route handlers delegate into typed server composition.
- Server API → Chat service: includes · Server root composes chat domain.
- Server API → Message service: includes · Server root composes message domain.
- Server API → Presence service: includes · Server root composes presence domain.
- Server API → User service: includes · Server root composes user domain.
- Server API → File storage: includes · Server root composes file storage integration.
- Chat service → Shared contracts: maps · Chat DTOs and controllers align to shared contracts.
- Message service → Shared contracts: maps · Message DTOs and controllers align to shared contracts.
- Presence service → Client libs: uses · Presence relies on Redis and realtime helpers.
- Message service → Prisma DB: persists · Messages are stored through Prisma.
- Chat service → Prisma DB: persists · Chats and join state are stored through Prisma.
- User service → Prisma DB: persists · Users are stored through Prisma.
- File storage → Prisma DB: references · Attachment metadata is associated with persisted message records.
- Contexts → Client libs: backs · Contexts are driven by shared client utilities and providers.