# maarcusrenierol/real-time-chat-app architecture

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

- Interactive diagram: https://gitdiagram.com/maarcusrenierol/real-time-chat-app
- Repository: https://github.com/maarcusrenierol/real-time-chat-app

## Overview

##### Main Components

a. Client Layer:
- Next.js Frontend (src/app directory)
- Component Structure (/components)
- Client-side State Management (stores)

b. API Layer:
- tRPC API endpoints (/server/routers)
- Authentication endpoints (Kinde Auth)
- Pusher integration

c. Server Layer:
- tRPC Server
- Route Handlers
- Business Logic

d. Database Layer:
- Drizzle ORM Database (SQL)
- Redis Database (for caching/real-time features)

##### Client → Server Communication
- TRPC client-server connection
- WebSocket connection via Pusher
- Authentication flow through Kinde

##### Data Flow
- Real-time message routing through Pusher
- Database operations through Drizzle
- Caching layer with Redis

##### Component Organization
- Dashboard layout structure
- Chat functionality
- Contact management
- Authentication flow

##### Connection types

**[Client Layer]**
- ↓↑
**[API Gateway Layer]**
- ↓↑
**[Server Layer]**
- ↓↑
**[Database Layer]**

##### Important subsystems to detail
- Chat system architecture
- Contact management flow
- Real-time notification system
- Authentication and authorization flow
- Database schema relationships

##### Specific components
- Next.js App Router structure
- tRPC router configuration
- Pusher real-time messaging
- Redis caching layer
- SQL database with Drizzle ORM
- Authentication middleware
- Frontend state management

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        NextFront["Next.js Frontend"]
        Components["Component Structure"]
        StateManagement["Client State Management"]
        DashLayout["Dashboard Layout"]
        
        subgraph ChatUI["Chat System"]
            ChatComp["Chat Components"]
            ContactComp["Contact Components"]
            NotifUI["Notification UI"]
        end
    end

    subgraph APIGateway["API Gateway Layer"]
        TRPCAPI["tRPC API Endpoints"]
        AuthAPI["Authentication API"]
        PusherAuth["Pusher Auth"]
    end

    subgraph ServerLayer["Server Layer"]
        TRPCServer["tRPC Server"]
        NotifListener["Notification Listener"]
        TypeDefs["Type Definitions"]
    end

    subgraph External["External Services"]
        Pusher["Pusher Service"]
        KindeAuth["Kinde Auth"]
    end

    subgraph DataLayer["Database Layer"]
        DrizzleDB[(Drizzle SQL DB)]
        RedisDB[(Redis Cache)]
    end

    %% Connections
    NextFront <--> TRPCAPI
    Components --> NextFront
    StateManagement --> NextFront
    ChatUI --> Components
    DashLayout --> NextFront

    TRPCAPI <--> TRPCServer
    AuthAPI <--> KindeAuth
    PusherAuth <--> Pusher

    TRPCServer --> DrizzleDB
    TRPCServer --> RedisDB
    NotifListener --> Pusher
    
    Pusher -.-> ChatUI
    KindeAuth -.-> AuthAPI

    %% Click events
    click NextFront "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/app/(dashboard)"
    click Components "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/components"
    click StateManagement "https://github.com/MaarcusRenieroL/real-time-chat-app/blob/main/src/lib/stores/use-notification-store.ts"
    click TRPCAPI "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/server/routers"
    click AuthAPI "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/app/api/auth"
    click PusherAuth "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/app/api/pusher/auth"
    click TRPCServer "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/server/trpc"
    click DrizzleDB "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/lib/db/drizzle"
    click RedisDB "https://github.com/MaarcusRenieroL/real-time-chat-app/blob/main/src/lib/db/redis/db.ts"
    click ChatComp "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/components/dashboard/chat"
    click ContactComp "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/components/dashboard/contacts"
    click NotifListener "https://github.com/MaarcusRenieroL/real-time-chat-app/blob/main/src/context/notification-listener.tsx"
    click NotifUI "https://github.com/MaarcusRenieroL/real-time-chat-app/blob/main/src/components/dashboard/navigation/notification-popover.tsx"
    click DashLayout "https://github.com/MaarcusRenieroL/real-time-chat-app/blob/main/src/app/(dashboard)/layout.tsx"
    click TypeDefs "https://github.com/MaarcusRenieroL/real-time-chat-app/tree/main/src/lib/types"

    %% Styling
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef api fill:#2ecc71,stroke:#27ae60,color:white
    classDef server fill:#e74c3c,stroke:#c0392b,color:white
    classDef external fill:#f1c40f,stroke:#f39c12,color:black
    classDef database fill:#9b59b6,stroke:#8e44ad,color:white

    class NextFront,Components,StateManagement,ChatUI,DashLayout frontend
    class TRPCAPI,AuthAPI,PusherAuth api
    class TRPCServer,NotifListener,TypeDefs server
    class Pusher,KindeAuth external
    class DrizzleDB,RedisDB database
```
