# adharshgamingyt/messengercraft architecture

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

- Interactive diagram: https://gitdiagram.com/adharshgamingyt/messengercraft
- Repository: https://github.com/adharshgamingyt/messengercraft

## Overview

##### Project type and purpose
- MessengerCraft is a full-stack web application (built on Next.js + React + TypeScript) providing real-time messaging, AI-driven suggestions, group planning tools, and end-to-end encryption. Although the README mentions cross-platform support (Web, iOS, Android), the provided codebase focuses on a Next.js web frontend with serverless API routes and a Postgres/Drizzle backend.

##### File structure
- Top-level config files: next.config.ts, drizzle.config.ts, auth.config.ts, package.json, tsconfig.json
- Frontend (src/app + src/components):
· “src/app” houses page-level routes (app router), nested layouts, and API-endpoints under “src/app/api”.
· “src/components” is divided into “my ui” (project-specific composites) and “ui” (shadcn/radix base components).
- Backend/DB (src/db, drizzle, src/schema):
· Drizzle migration scripts & meta files in “drizzle/”
· Database access & schema definitions in “src/db” and “src/schema”
· Seed/migrate scripts, Drizzle ORM for typesafe SQL
- Auth & Security:
· NextAuth (Auth.js) under “src/app/api/auth/[...nextauth]/route.ts”
· JWT & custom MeeC encryption logic in “auth.ts” and “lib/auth.ts”
- Utilities & Services:
· src/lib/utils.ts, custom hooks in src/hooks
· Real-time comm may leverage WebSockets or Server-Sent Events via API routes (no explicit socket folder, but pattern in “src/app/api/[[...route]]/route.ts”)
- Other: public assets, environment samples (.env.example), tests, docs.

##### Key insights from README
- Tech Stack: Next.js (React), TailwindCSS, Drizzle ORM + Postgres, Auth.js, custom encryption protocol, AI integration (e.g., OpenAI), Drizzle “Real-time Communication,” Neon Serverless Postgres.
- Core features: real-time messaging, AI replies, group planning, E2EE, file sharing.

a. Main components to model:
- Clients: Web Browser (Next.js frontend), Mobile Apps (iOS/Android)
- Next.js Server:
– App Router & SSR/SSG Layer (renders React pages)
– API Layer (serverless functions under src/app/api)
- Authentication Service: NextAuth.js/JWT module
- Encryption Engine: MeeC protocol module (client + server hooks)
- Real-time Messaging Engine: WebSockets / Drizzle pub/sub layer
- AI Service: External AI-provider (e.g., OpenAI)
- Database: Neon Serverless Postgres via Drizzle ORM
- File Storage Service: e.g. S3 or CDN for media uploads
b. Relationships & data flows:
– User → (HTTPS) → Next.js Frontend → SSR/CSR pages
– Frontend → (HTTPS/API) → Next.js API Routes → Auth module → DB
– Messaging: Frontend ↔ (WebSocket/SSE) ↔ Real-time Engine ↔ Postgres
– AI requests: Messaging Engine/API → External AI Service → Response
– File upload/download: Frontend → File Storage → CDN → Frontend
– Encryption: Messages encrypted/decrypted at client ↔ server for metadata
c. Architectural patterns & principles:
– Modular UI components (shadcn/radix → “ui” library)
– Feature folders (Auth, Home, Download, On-boarding under src/app)
– API-First serverless functions (Next.js App Router)
– Typesafe ORM (Drizzle) + migration snapshots
– Hooks and utilities to centralize cross-cutting concerns (auth, user state)
d. Technologies & frameworks:
– Next.js App Router, React, TypeScript, TailwindCSS
– Auth.js (NextAuth) for session mgmt
– Drizzle ORM + Neon Postgres
– AI provider (e.g. OpenAI) via REST APIs
– Encryption via custom MeeC protocol libraries
– Testing with Jest, linting with ESLint/Prettier

##### Elements
· Blue for user-facing layers (UI, Mobile Apps)
· Green for backend services (API, Auth, Encryption)
· Orange for data stores (Postgres, CDN)
· Purple for third-party/external (AI, S3)
- Annotations for key frameworks (Next.js, Drizzle, Tailwind, Auth.js)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph "Clients" 
        direction TB
        WebBrowser["Web Browser"]:::frontend
        MobileApps["Mobile Apps (iOS/Android)"]:::frontend
    end

    %% Frontend Layer
    subgraph "Frontend (Next.js + React)" 
        direction TB
        AppRouter["App Router & SSR/CSR (src/app)"]:::frontend
        MyUI["Project UI Components (src/components/my ui)"]:::frontend
        BaseUI["Base UI Library (src/components/ui)"]:::frontend
        UtilsHooks["Utilities & Hooks"]:::frontend
        GlobalStyles["Global Styles & Middleware"]:::frontend
        ConfigTypes["Site Config & Types"]:::frontend
    end

    %% API Layer
    subgraph "Serverless API Layer" 
        direction TB
        APIRoutes["API Routes (src/app/api)"]:::service
        AuthRoute["NextAuth Route (src/app/api/auth/[...nextauth]/route.ts)"]:::service
        RealtimeAPI["Real-time Engine Route (src/app/api/[[...route]]/route.ts)"]:::service
    end

    %% Auth & Encryption Services
    subgraph "Auth & Encryption" 
        direction TB
        AuthConfig["Auth Config (auth.config.ts)"]:::service
        JWTEngine["Encryption Engine (auth.ts)"]:::service
        AuthLib["Encryption Hooks (src/lib/auth.ts)"]:::service
    end

    %% Database Layer
    subgraph "Database & ORM" 
        direction TB
        DrizzleConfig["Drizzle Config (drizzle.config.ts)"]:::service
        Migrations["Migrations (drizzle/)"]:::service
        DBAccess["DB Access & Schema"]:::service
    end

    %% Data Stores
    subgraph "Data Stores"
        direction TB
        Postgres["Neon Postgres"]:::datastore
        FileStore["File Storage (S3/CDN)"]:::external
    end

    %% External Services
    subgraph "External Services"
        direction TB
        AIService["AI Provider (OpenAI)"]:::external
    end

    %% Connections
    WebBrowser -->|"HTTPS Request"| AppRouter
    MobileApps -->|"HTTPS Request"| AppRouter

    AppRouter -->|"API Call"| APIRoutes
    APIRoutes -->|"Invoke"| AuthRoute
    APIRoutes -->|"Invoke"| RealtimeAPI

    AuthRoute -->|"Validate Session"| AuthConfig
    AuthRoute -->|"Query"| DBAccess

    JWTEngine -->|"Encrypt/Decrypt"| WebBrowser
    JWTEngine -->|"Encrypt/Decrypt Metadata"| AuthRoute
    AuthLib -->|"Provide Helpers"| AuthRoute

    RealtimeAPI -->|"Pub/Sub"| DBAccess
    RealtimeAPI -->|"WebSocket/SSE"| WebBrowser

    APIRoutes -->|"Utility Functions"| UtilsHooks
    AppRouter -->|"Use Hooks"| UtilsHooks

    DBAccess -->|"Run Migrations"| DrizzleConfig
    DrizzleConfig -->|"Manage"| Migrations
    DBAccess -->|"Read/Write"| Postgres

    APIRoutes -->|"AI Request"| AIService
    RealtimeAPI -->|"AI Reply"| AIService

    WebBrowser -->|"Upload/Download"| FileStore
    APIRoutes -->|"Presigned URLs"| FileStore

    AppRouter -->|"Load Styles"| GlobalStyles
    AppRouter -->|"Use Config & Types"| ConfigTypes

    %% Click Events
    click AppRouter "https://github.com/adharshgamingyt/messengercraft/tree/main/src/app"
    click MyUI "https://github.com/adharshgamingyt/messengercraft/tree/main/src/components/my ui"
    click BaseUI "https://github.com/adharshgamingyt/messengercraft/tree/main/src/components/ui"
    click APIRoutes "https://github.com/adharshgamingyt/messengercraft/tree/main/src/app/api"
    click AuthRoute "https://github.com/adharshgamingyt/messengercraft/blob/main/src/app/api/auth/[...nextauth]/route.ts"
    click RealtimeAPI "https://github.com/adharshgamingyt/messengercraft/blob/main/src/app/api/[[...route]]/route.ts"
    click AuthConfig "https://github.com/adharshgamingyt/messengercraft/blob/main/auth.config.ts"
    click JWTEngine "https://github.com/adharshgamingyt/messengercraft/blob/main/auth.ts"
    click AuthLib "https://github.com/adharshgamingyt/messengercraft/blob/main/src/lib/auth.ts"
    click DrizzleConfig "https://github.com/adharshgamingyt/messengercraft/blob/main/drizzle.config.ts"
    click Migrations "https://github.com/adharshgamingyt/messengercraft/tree/main/drizzle/"
    click DBAccess "https://github.com/adharshgamingyt/messengercraft/blob/main/src/db/index.ts"
    click DBAccess "https://github.com/adharshgamingyt/messengercraft/blob/main/src/db/migrate.ts"
    click DBAccess "https://github.com/adharshgamingyt/messengercraft/blob/main/src/db/schema.ts"
    click DBAccess "https://github.com/adharshgamingyt/messengercraft/blob/main/src/db/seed.ts"
    click DBAccess "https://github.com/adharshgamingyt/messengercraft/blob/main/src/schema/index.ts"
    click UtilsHooks "https://github.com/adharshgamingyt/messengercraft/blob/main/src/lib/utils.ts"
    click UtilsHooks "https://github.com/adharshgamingyt/messengercraft/blob/main/src/hooks/use-current-user.ts"
    click UtilsHooks "https://github.com/adharshgamingyt/messengercraft/blob/main/src/hooks/use-mobile.ts"
    click GlobalStyles "https://github.com/adharshgamingyt/messengercraft/blob/main/src/app/globals.css"
    click GlobalStyles "https://github.com/adharshgamingyt/messengercraft/blob/main/src/middleware.ts"
    click ConfigTypes "https://github.com/adharshgamingyt/messengercraft/blob/main/src/config/siteconfig.ts"
    click ConfigTypes "https://github.com/adharshgamingyt/messengercraft/blob/main/src/types/types.d.ts"

    %% Styles
    classDef frontend fill:#D0EBFF,stroke:#1C7ED6,color:#1C7ED6;
    classDef service fill:#E6FCF5,stroke:#37B24D,color:#2F9E44;
    classDef datastore fill:#FFF3BF,stroke:#F08C00,color:#F08C00;
    classDef external fill:#F3E6FF,stroke:#7950F2,color:#5F3DC4;
```
