# vanhoaltw/next-chat architecture

> Architecture diagram of the GitHub repository vanhoaltw/next-chat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vanhoaltw/next-chat
- Repository: https://github.com/vanhoaltw/next-chat

## Overview

##### Project type and purpose
- This is a full-stack, real-time chat application built on Next.js 14 with React and TypeScript.
- It supports 1-on-1 and group messaging, image uploads, OAuth login (Google/GitHub), and real-time updates via Pusher.
- The app uses MongoDB (via Mongoose) as its primary data store and NextAuth for authentication.

##### File structure
- Top level
- next.config.js, package.json, tsconfig.json, etc.
- public/ — static assets (favicon, sounds)
- src/app/ — Next.js App Router pages and serverless API endpoints
- app/api/auth/[...nextauth]/route.ts — NextAuth handler
- app/api/conversation, message, user/.../route.ts — CRUD REST endpoints
- app/chat/(desktop) vs app/chat/(mobile) — platform-specific UI entry points
- page.tsx, layout.tsx, provider.tsx — root layout, global providers (theme, auth session)
- src/components/ — shared UI primitives (chat bubbles, input controls, conversation lists)
- src/lib/ — utilities and wrappers
- mongoDb.ts — Mongoose connection
- auth.ts / session.ts — NextAuth helpers
- publisher.server.ts & publisher.client.ts — Pusher server- and client-side abstractions
- src/server/ — pure back-end models and DB connection helpers
- models/Account.ts, Conversation.ts, Message.ts, User.ts
- src/service/index.ts — business-logic layer bridging API routes to models
- src/hooks/, src/styles/, src/types/ — support code, styles, TypeScript types

##### README for additional insights
- Features list confirms real-time messaging and OAuth.
- Technology stack calls out Next.js App Router, NextAuth, Pusher, MongoDB, TailwindCSS, TypeScript.

a. Client (Browser)
- Next.js pages and React components rendered on the client
- Pusher client for subscribing to real-time events
- Image upload integration with imgbb API
b. Next.js Serverless Layer
- App Router server components handling page-level SSR/SSG
- API routes under /app/api for auth, conversation, message, user
- NextAuth OAuth flow with GitHub/Google providers
c. Business Logic Layer
- src/service functions encapsulating operations (create message, list conversations, etc.)
d. Data Layer
- MongoDB cluster accessed via Mongoose models (User, Conversation, Message, Account)
- DB connection management in mongoDb.ts
e. Real-Time Layer
- Pusher Server: publishes events from API route handlers
- Pusher Client: subscribes in React to new-message channels
f. External Services
- OAuth providers (Google, GitHub) for login
- imgbb API for image uploads
- Pusher hosted service for WebSockets

##### Relationships and interactions
- User → Browser UI (React)
- Browser UI → Next.js API routes (REST calls via fetch)
- API route handler → service layer → Mongoose models → MongoDB
- API route handler → Pusher server SDK → Pusher channels
- Browser UI → Pusher client → real-time event streams
- Browser UI → imgbb REST API → receives image URLs → sends via API
- NextAuth route → OAuth provider → returns session token → stored in cookie/session

##### Architectural patterns and design principles
- Layered architecture: Presentation (React), API (Next.js serverless), Service, Data (MongoDB)
- File-based routing (Next.js App Router) separates SSR pages and API endpoints
- Single-responsibility in folders: components, lib, server, service, hooks
- Real-time pub/sub decouples message persistence from delivery

##### Final checklist
- Clear, descriptive labels on every component
- Related files/functions into their logical layers

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser Client Layer
    subgraph "Browser Client" 
        direction TB
        Page["Root Pages & Providers"]:::frontend
        Layout["Root Layout"]:::frontend
        Provider["Auth & Theme Provider"]:::frontend
        UI["Chat UI Entry Points"]:::frontend
        SharedUI["Shared UI Primitives"]:::frontend
        Request["API Fetch Layer"]:::frontend
        PusherClient["Pusher Client"]:::realtime
    end

    %% Next.js Serverless Layer
    subgraph "Next.js Serverless" 
        direction TB
        NextAuthHandler["NextAuth Handler"]:::backend
        ConvListCreate["Conversation List/Create API"]:::backend
        ConvDetail["Conversation Detail API"]:::backend
        MessageAPI["Message API"]:::backend
        UserAPI["User API"]:::backend
    end

    %% Service Layer
    Service["Business Logic Layer"]:::service

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        MongoDbLib["MongoDB Connection"]:::service
        DbConnect["DB Connect Helper"]:::service
        ModelAccount["Account Model"]:::db
        ModelConversation["Conversation Model"]:::db
        ModelMessage["Message Model"]:::db
        ModelUser["User Model"]:::db
    end

    %% Real-Time Layer
    PusherServer["Pusher Server"]:::realtime

    %% External Services
    subgraph "External Services"
        direction TB
        OAuthGoogle["Google OAuth"]:::external
        OAuthGitHub["GitHub OAuth"]:::external
        Imgbb["imggb Image API"]:::external
    end

    %% Interactions
    Page -->|renders| UI
    UI -->|fetch /api/...| Request
    Request -->|POST /api/conversation| ConvListCreate
    Request -->|GET/PUT/DELETE /api/conversation/:id| ConvDetail
    Request -->|POST/GET /api/message| MessageAPI
    Request -->|GET /api/user| UserAPI

    ConvListCreate -->|"calls"| Service
    ConvDetail -->|"calls"| Service
    MessageAPI -->|"calls"| Service
    UserAPI -->|"calls"| Service

    Service -->|"uses"| ModelAccount
    Service -->|"uses"| ModelConversation
    Service -->|"uses"| ModelMessage
    Service -->|"uses"| ModelUser
    Service -->|connect via| MongoDbLib
    Service -->|connect via| DbConnect

    ConvListCreate -->|publish new-message| PusherServer
    MessageAPI -->|publish new-message| PusherServer

    PusherServer -->|new-message events| PusherClient

    Page -->|initiates auth| NextAuthHandler
    NextAuthHandler -->|OAuth2 flow| OAuthGoogle
    NextAuthHandler -->|OAuth2 flow| OAuthGitHub
    NextAuthHandler -->|sets session| Provider

    UI -->|upload image| Imgbb
    Imgbb -->|returns URL| UI

    %% Click Events
    click Page "https://github.com/vanhoaltw/next-chat/blob/main/src/app/page.tsx"
    click Layout "https://github.com/vanhoaltw/next-chat/blob/main/src/app/layout.tsx"
    click Provider "https://github.com/vanhoaltw/next-chat/blob/main/src/app/provider.tsx"
    click UI "https://github.com/vanhoaltw/next-chat/blob/main/src/app/chat/(desktop)/index.tsx"
    click PusherClient "https://github.com/vanhoaltw/next-chat/blob/main/src/lib/pubsher.client.ts"
    click NextAuthHandler "https://github.com/vanhoaltw/next-chat/blob/main/src/app/api/auth/[...nextauth]/route.ts"
    click ConvListCreate "https://github.com/vanhoaltw/next-chat/blob/main/src/app/api/conversation/route.ts"
    click ConvDetail "https://github.com/vanhoaltw/next-chat/blob/main/src/app/api/conversation/[id]/route.ts"
    click MessageAPI "https://github.com/vanhoaltw/next-chat/blob/main/src/app/api/message/route.ts"
    click UserAPI "https://github.com/vanhoaltw/next-chat/blob/main/src/app/api/user/route.ts"
    click Service "https://github.com/vanhoaltw/next-chat/blob/main/src/service/index.ts"
    click MongoDbLib "https://github.com/vanhoaltw/next-chat/blob/main/src/lib/mongoDb.ts"
    click DbConnect "https://github.com/vanhoaltw/next-chat/blob/main/src/server/dbConnect.ts"
    click ModelAccount "https://github.com/vanhoaltw/next-chat/blob/main/src/server/models/Account.ts"
    click ModelConversation "https://github.com/vanhoaltw/next-chat/blob/main/src/server/models/Conversation.ts"
    click ModelMessage "https://github.com/vanhoaltw/next-chat/blob/main/src/server/models/Message.ts"
    click ModelUser "https://github.com/vanhoaltw/next-chat/blob/main/src/server/models/User.ts"
    click PusherServer "https://github.com/vanhoaltw/next-chat/blob/main/src/lib/pubsher.server.ts"
    click SharedUI "https://github.com/vanhoaltw/next-chat/tree/main/src/components"
    click Request "https://github.com/vanhoaltw/next-chat/blob/main/src/lib/request.ts"
    click UI "https://github.com/vanhoaltw/next-chat/blob/main/src/app/chat/(mobile)/index.tsx"
    click SharedUI "https://github.com/vanhoaltw/next-chat/tree/main/src/components/chat/chat-bubble"
    click SharedUI "https://github.com/vanhoaltw/next-chat/tree/main/src/components/chat/chat-input"
    click SharedUI "https://github.com/vanhoaltw/next-chat/tree/main/src/components/chat/chat-list"
    click SharedUI "https://github.com/vanhoaltw/next-chat/tree/main/src/components/conversation"
    click SharedUI "https://github.com/vanhoaltw/next-chat/blob/main/src/components/Responsive.tsx"
    click SharedUI "https://github.com/vanhoaltw/next-chat/blob/main/src/components/ThemeProvider.tsx"
    click Imgbb "https://github.com/vanhoaltw/next-chat/blob/main/src/components/chat/chat-input/FileUpload.tsx"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#00529B,color:#000
    classDef backend fill:#DFF2BF,stroke:#4F8A10,color:#000
    classDef service fill:#FFF2CC,stroke:#9F6000,color:#000
    classDef db fill:#E6E6FA,stroke:#800080,color:#000
    classDef realtime fill:#FFE0B2,stroke:#FF6F00,color:#000
    classDef external fill:#F5F5F5,stroke:#666,color:#000
```
