# sujal8976/rtchat architecture

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

- Interactive diagram: https://gitdiagram.com/sujal8976/rtchat
- Repository: https://github.com/sujal8976/rtchat

## Overview

##### Main System Components

A. Client Layer
- Next.js Frontend (apps/web)
- WebSocket Client Connection
- Zustand State Management
- Authentication Flow

B. Server Layer
- Next.js API Routes (HTTP Server)
- WebSocket Server (apps/ws)
- Authentication Service

C. Database Layer
- PostgreSQL Database
- Prisma ORM

##### Detailed Component Breakdown

Frontend Components (apps/web):
- Authentication components (login/register)
- Chat interface (messages, rooms, members)
- Sidebar and navigation
- Real-time communication handlers

Backend Services:
- WebSocket Server components:
- Connection handler
- Message service
- Room service
- Authentication service

Database Schema:
- Main entities:
- Users
- Rooms
- Messages
- Room memberships

##### Connection Flows

**a. Authentication Flow:**
**User -> Next.js API -> Auth Service -> Database**

**b. Real-time Communication Flow:**
**Client -> WebSocket Connection -> WS Server -> Database**
- > Broadcast to Room Members

**c. Room Management Flow:**
**Client -> HTTP API -> Database**
- > WebSocket -> Room Service

##### Specific Implementation Details

Architectural patterns:
- Singleton WebSocket connections
- Debouncing for API calls
- Infinite scroll implementation
- Database indexing
- State management flow

##### Technical Specifications

- Next.js 13+ (App Router)
- WebSocket (ws library)
- PostgreSQL
- Prisma ORM
- Zustand
- TailwindCSS
- NextAuth

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        NextApp["Next.js Web Application"]
        UI["UI Components"]
        Store["Client State Management"]
        WSClient["WebSocket Client"]
        ChatComp["Chat Components"]
        AuthUI["Authentication UI"]
        WSProvider["WebSocket Provider"]
        
        NextApp --> UI
        NextApp --> Store
        NextApp --> WSClient
        NextApp --> ChatComp
        NextApp --> AuthUI
        NextApp --> WSProvider
        WSProvider --> WSClient
    end

    subgraph API
        APIRoutes["API Routes"]
        AuthService["Authentication Service"]
        RoomActions["Room Management API"]
        
        APIRoutes --> AuthService
        APIRoutes --> RoomActions
    end

    subgraph WebSocket
        WSServer["WebSocket Server"]
        WSAuth["WS Auth Service"]
        WSRoom["Room Service"]
        WSMessage["Message Service"]
        
        WSServer --> WSAuth
        WSServer --> WSRoom
        WSServer --> WSMessage
    end

    subgraph Data
        PrismaORM["Prisma ORM"]
        PostgreSQL["PostgreSQL DB"]
        DBSchema["Database Schema"]
        
        PrismaORM --> PostgreSQL
        DBSchema --> PostgreSQL
    end

    Client --> API
    Client --> WebSocket
    API --> Data
    WebSocket --> Data

    %% Component Mappings
    click NextApp "https://github.com/sujal8976/rtchat/tree/main/apps/web"
    click UI "https://github.com/sujal8976/rtchat/tree/main/packages/ui/src/components"
    click Store "https://github.com/sujal8976/rtchat/tree/main/apps/web/lib/store"
    click WSClient "https://github.com/sujal8976/rtchat/blob/main/apps/web/lib/services/websocket.ts"
    click APIRoutes "https://github.com/sujal8976/rtchat/tree/main/apps/web/app/api"
    click WSServer "https://github.com/sujal8976/rtchat/tree/main/apps/ws"
    click AuthService "https://github.com/sujal8976/rtchat/tree/main/apps/web/app/api/auth"
    click WSAuth "https://github.com/sujal8976/rtchat/blob/main/apps/ws/src/services/auth.service.ts"
    click PrismaORM "https://github.com/sujal8976/rtchat/tree/main/packages/db"
    click ChatComp "https://github.com/sujal8976/rtchat/tree/main/apps/web/components/chat"
    click WSMessage "https://github.com/sujal8976/rtchat/blob/main/apps/ws/src/services/message.service.ts"
    click AuthUI "https://github.com/sujal8976/rtchat/tree/main/apps/web/components/auth"
    click RoomActions "https://github.com/sujal8976/rtchat/tree/main/apps/web/lib/actions/room"
    click WSRoom "https://github.com/sujal8976/rtchat/blob/main/apps/ws/src/services/room.service.ts"
    click DBSchema "https://github.com/sujal8976/rtchat/blob/main/packages/db/prisma/schema.prisma"
    click WSProvider "https://github.com/sujal8976/rtchat/blob/main/apps/web/providers/websocketProvider.tsx"

    classDef client fill:#2374ab
    classDef api fill:#2a9d8f
    classDef websocket fill:#e9c46a
    classDef data fill:#8338ec
    
    class NextApp,UI,Store,WSClient,ChatComp,AuthUI,WSProvider client
    class APIRoutes,AuthService,RoomActions api
    class WSServer,WSAuth,WSRoom,WSMessage websocket
    class PrismaORM,PostgreSQL,DBSchema data
```
