# varadarajan-m/vi-messenger architecture

> Architecture diagram of the GitHub repository varadarajan-m/vi-messenger, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/varadarajan-m/vi-messenger
- Repository: https://github.com/varadarajan-m/vi-messenger

## Overview

##### Project Type and Purpose
- VI Messenger is a real-time messaging application that supports personal and group chats with rich features such as AI chat with LLAMA-3, file sharing (multiple formats), message replies, real-time indicators (typing, online/offline), notifications, and PWA capabilities.
- The backend is implemented with Node.js, Express, and Socket.io combined with MongoDB for data persistence.
- The frontend is built with React, TypeScript, Tailwind CSS, and Zustand, providing a responsive and dynamic user interface.

##### Overall File Structure
- The project is organized into two main directories under the “apps” folder:
- apps/server: Contains all backend code. Notable directories include:
▸ controllers – handling business logic for authentication, chatting, messages, users, etc.
▸ routes – defining API endpoints (auth, chat, message, etc.)
▸ middlewares – handling request validation, authentication, and any additional processing.
▸ models – representing the MongoDB data structures (chat, message, user).
▸ db – managing the database connection.
▸ socket.ts – handling real-time websocket (Socket.io) connections.
- apps/web: Contains the complete frontend React application.
▸ src – includes components for authentication and chat (both private and AI chat), hooks for state management, API calls, utilities, and PWA-related files.
▸ Provided assets, configuration files, and service worker registration for PWA functionality.

a. Main Components:
- Client (Frontend): Represented by the “apps/web” project built in React (with TypeScript, TailwindCSS) that includes UI components, state management through Zustand, and API integrations.
- Server (Backend): Represented by the “apps/server” portion developed with Node.js and Express. It encompasses:
- API Controllers and Routes: Separate modules for handling user authentication, chat operations, messaging, etc.
- Middlewares: For authentication and request processing.
- Socket Management: Real-time communication via Socket.io.
- Database Connectivity: MongoDB connection handled in the “db” module.
- Database: MongoDB serves as the data persistence layer.
- External/AI Service (if applicable): Although implemented internally, the AI chat feature can be highlighted as a processing module (using LLAMA-3) that may interact with additional backend AI services or libraries.

b. Relationships and Data Flow:
- The browser-based client communicates with the server via two primary channels:
- HTTP REST API calls (for actions like login, registration, getting chat history, file uploads, etc.)
- WebSocket (Socket.io) for live messaging, typing indicators, and real-time notifications.
- On the server, API requests are first handled by middlewares (e.g., for authorization) and then routed to the appropriate controllers.
- Controllers interact with the data models to read/write from the MongoDB database.
- The chat and AI features are managed via dedicated modules that process and stream responses back to the client.

c. Architectural Patterns and Principles:
- Separation of concerns is clearly maintained: the frontend and backend are decoupled, each with its own workspace.
- MVC-like separation in the backend: controllers, models, and routes.
- Real-time communication using WebSockets.
- PWA architecture considerations in the frontend (service worker, manifest, offline indicators).
- Modular design: clear separation of utilities, validators, and custom hooks (for React).

d. Relevant Technologies:
- Frontend: React with TypeScript, Zustand (state management), Tailwind CSS, and Vite for bundling.
- Backend: Node.js, Express, Socket.io, TypeScript, and MongoDB.
- Additional libraries: ESLint configurations for code quality, custom validators, custom middlewares, and logger utilities.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Frontend (apps/web)
    subgraph "Frontend (apps/web)"
        UI["UI Components (React)"]
        ST["State Management (Zustand)"]
        APIInt["API Integration (HTTP)"]
        AIC["AI Chat Components"]
        MFT["Manifest"]
        SW["Service Worker"]
    end

    %% Backend (apps/server)
    subgraph "Backend (apps/server)"
        AR["API Routes (Express)"]
        AC["API Controllers"]
        AM["Auth Middlewares"]
        CM["Chat Middlewares"]
        SM["Socket Manager (Socket.io)"]
        DC["Database Connection"]
        DM["Data Models"]
    end

    %% Database
    DB["MongoDB"]

    %% Relationships - Client Side
    UI -->|"calls"| APIInt
    UI ---|"state"| ST
    UI ---|"realTime"| SM

    %% Relationships - Frontend to Backend (HTTP)
    APIInt -->|"HTTP"| AR

    %% Relationships - Backend internal routing
    AR -->|"uses"| AM
    AR -->|"uses"| CM
    AR -->|"routesTo"| AC
    AC -->|"accesses"| DM
    DM -->|"via"| DC
    DC -->|"connects"| DB

    %% Additional real-time arrow from Socket Manager to UI Components
    SM -->|"emits"| UI

    %% Styling
    classDef frontend fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef backend fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef database fill:#ffd700,stroke:#000,stroke-width:2px;
    
    class UI,ST,APIInt,AIC,MFT,SW frontend;
    class AR,AC,AM,CM,SM,DC,DM backend;
    class DB database;

    %% Click Events for Frontend Components
    click UI "https://github.com/varadarajan-m/vi-messenger/tree/main/apps/web/src/components"
    click ST "https://github.com/varadarajan-m/vi-messenger/blob/main/apps/web/src/zustand/store.ts"
    click APIInt "https://github.com/varadarajan-m/vi-messenger/tree/main/apps/web/src/api"
    click AIC "https://github.com/varadarajan-m/vi-messenger/tree/main/apps/web/src/components/ai-chat"
    click MFT "https://github.com/varadarajan-m/vi-messenger/blob/main/apps/web/assets/manifest.json"
    click SW "https://github.com/varadarajan-m/vi-messenger/blob/main/apps/web/service-worker.js"

    %% Click Events for Backend Components
    click AC "https://github.com/varadarajan-m/vi-messenger/tree/main/apps/server/src/controllers"
    click AR "https://github.com/varadarajan-m/vi-messenger/tree/main/apps/server/src/routes"
    click AM "https://github.com/varadarajan-m/vi-messenger/blob/main/apps/server/src/middlewares/auth.ts"
    click CM "https://github.com/varadarajan-m/vi-messenger/blob/main/apps/server/src/middlewares/chat.ts"
    click DC "https://github.com/varadarajan-m/vi-messenger/blob/main/apps/server/src/db/connection.ts"
    click SM "https://github.com/varadarajan-m/vi-messenger/blob/main/apps/server/src/socket.ts"
    click DM "https://github.com/varadarajan-m/vi-messenger/tree/main/apps/server/src/models"
```
