# visris-19/baatify architecture

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

- Interactive diagram: https://gitdiagram.com/visris-19/baatify
- Repository: https://github.com/visris-19/baatify

## Overview

##### Project type and purpose
- **Type**: Full-stack real-time chat application (WhatsApp-like clone) with stories.
- **Purpose**: Enable authenticated users to exchange messages in one-on-one chats, post 24-hour stories, and view user profiles with real-time updates via WebSockets.

##### File structure
- Top-level split into two directories: **Backend/** and **Frontend/**.
- **Backend/** follows an MVC-style layout:
- `models/` (MongoDB schemas)
- `controllers/` (Express request handlers)
- `routes/` (Express routers mapping URIs to controllers)
- `middleware/` (authentication, error handling, file upload)
- `index.js` (entry point that wires Express and Socket.IO)
- **Frontend/** is a Vite-powered React SPA:
- `src/components/` (feature-specific & shared UI components)
- `src/pages/` (route targets)
- `src/context/AuthContext.jsx` (React Context for auth state)
- `src/services/socket.js` & Axios calls (WebSocket & REST clients)
- `index.html` + `main.jsx` (bootstraps the React app)

##### README
- Confirms tech stack: Node.js, Express, MongoDB, Socket.IO, JWT; React, React Router, Tailwind, Axios.
- Describes main features (auth, chats, messages, stories).
- Lists API endpoints and Socket events.

##### Diagramming the architecture
a. **Main components**
- **Client (Browser/React SPA)**
- **API Layer (Express.js server)**
- **WebSocket Layer (Socket.IO server)**
- **Database (MongoDB)**
- **External Services** (e.g., optional object storage for story uploads, SMTP for email)
b. **Relationships & Interactions**
- React UI ←→ REST API (Axios over HTTP) for CRUD operations (auth, chats, messages, stories).
- React UI ←→ Socket.IO (WebSocket) for real-time events (new messages, online status, stories).
- Express controllers ←→ MongoDB via Mongoose models.
- Middleware enforces JWT authentication on protected REST endpoints and WebSocket handshakes.
c. **Architectural patterns & principles**
- **Backend**: MVC (Models → Controllers → Routes), layered separation (middleware, controllers, data).
- **Frontend**: Component-based architecture, Context API for global state (auth), PrivateRoute for protected views.
- **Real-time**: Pub/Sub pattern via Socket.IO.
- **Security**: JWT for stateless authentication, middleware for request validation.
d. **Key technologies**
- Node.js & Express.js, Mongoose/MongoDB, Socket.IO, JWT
- React, React Router, Tailwind CSS, Axios, Vite

##### Detailed checklist
- UI Layer: show key pages (Login/Register, Chat window, Stories, Profile) and context provider.
- Service Layer: illustrate `AuthController`, `ChatController`, `MessageController`, `StoryController`.
- Data Layer: MongoDB collections (Users, Chats, Messages, Stories).
- WebSocket Hub: single Socket.IO server instance hooked into the Express app.
- Middleware: JWT auth and error handler between routes and controllers.
- External boundaries: user’s browser, database server, (optional) cloud file storage.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (React SPA)" 
        direction TB
        FE_Entrypoint["index.html + main.jsx + App.jsx"]:::frontend
        AuthContext["AuthContext.jsx"]:::frontend
        SocketClient["socket.js"]:::frontend
        PrivateRoute["PrivateRoute.jsx"]:::frontend

        subgraph "Pages" 
            direction TB
            LoginPage["Login.jsx"]:::frontend
            RegisterPage["Register.jsx"]:::frontend
            ChatPage["Chat.jsx"]:::frontend
            ProfilePage["Profile.jsx"]:::frontend
            StoriesPage["Stories.jsx"]:::frontend
        end

        subgraph "Chat Components"
            direction TB
            ChatHeader["ChatHeader.jsx"]:::frontend
            ChatList["ChatList.jsx"]:::frontend
            ChatWindow["ChatWindow.jsx"]:::frontend
            ChatInput["ChatInput.jsx"]:::frontend
            MessageBubble["MessageBubble.jsx"]:::frontend
            SearchUsers["SearchUsers.jsx"]:::frontend
        end

        subgraph "Profile Components"
            direction TB
            ProfileHeader["ProfileHeader.jsx"]:::frontend
            ProfileSettings["ProfileSettings.jsx"]:::frontend
        end

        subgraph "Story Components"
            direction TB
            StoryUploader["StoryUploader.jsx"]:::frontend
            StoryViewer["StoryViewer.jsx"]:::frontend
        end
    end

    %% Backend Layer
    subgraph "Backend (Express + Socket.IO)"
        direction TB
        ServerEntry["index.js"]:::backendREST
        subgraph "Middleware"
            direction TB
            AuthMiddleware["auth.js"]:::backendREST
            ErrorMiddleware["error.js"]:::backendREST
            UploadMiddleware["upload.js"]:::backendREST
        end

        subgraph "Routes"
            direction TB
            AuthRoutes["auth.routes.js"]:::backendREST
            ChatRoutes["chat.routes.js"]:::backendREST
            MessageRoutes["message.routes.js"]:::backendREST
            StoryRoutes["story.routes.js"]:::backendREST
            UserRoutes["user.routes.js"]:::backendREST
        end

        subgraph "Controllers"
            direction TB
            AuthController["auth.controller.js"]:::backendREST
            ChatController["chat.controller.js"]:::backendREST
            MessageController["message.controller.js"]:::backendREST
            StoryController["story.controller.js"]:::backendREST
        end

        subgraph "Models"
            direction TB
            UserModel["User.js"]:::database
            ChatModel["Chat.js"]:::database
            MessageModel["Message.js"]:::database
            StoryModel["Story.js"]:::database
        end
    end

    %% Database
    MongoDB[(MongoDB)]:::database

    %% Interactions
    FE_Entrypoint -->|bootstraps App| AuthContext
    AuthContext -->|provides token| PrivateRoute
    FE_Entrypoint -->|configures WS| SocketClient
    PrivateRoute -->|guards pages| LoginPage
    PrivateRoute -->|guards pages| RegisterPage
    PrivateRoute -->|guards pages| ChatPage
    PrivateRoute -->|guards pages| ProfilePage
    PrivateRoute -->|guards pages| StoriesPage

    %% REST calls
    ChatPage -->|HTTP| AuthRoutes
    ProfilePage -->|HTTP| UserRoutes
    StoriesPage -->|HTTP| StoryRoutes
    LoginPage -->|HTTP POST /auth/login| AuthRoutes
    RegisterPage -->|HTTP POST /auth/register| AuthRoutes

    AuthRoutes -->|calls| AuthController
    ChatRoutes -->|calls| ChatController
    MessageRoutes -->|calls| MessageController
    StoryRoutes -->|calls| StoryController
    UserRoutes -->|calls| AuthController

    %% Middleware flow
    AuthRoutes --> AuthMiddleware --> AuthController
    ChatRoutes --> AuthMiddleware --> ChatController
    MessageRoutes --> AuthMiddleware --> MessageController
    StoryRoutes --> AuthMiddleware --> StoryController

    %% Controllers to Models
    AuthController -->|read/write| UserModel
    ChatController -->|read/write| ChatModel
    MessageController -->|read/write| MessageModel
    StoryController -->|read/write| StoryModel

    %% Database connection
    UserModel --> MongoDB
    ChatModel --> MongoDB
    MessageModel --> MongoDB
    StoryModel --> MongoDB

    %% WebSocket flow
    FE_Entrypoint -->|WS connect| ServerEntry
    ServerEntry -->|setup Socket.IO| SocketClient
    ServerEntry -->|authenticate WS| AuthMiddleware
    ServerEntry -->|emit/on events| ChatController
    ServerEntry -->|emit/on events| MessageController
    ServerEntry -->|emit/on events| StoryController

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#1c4e80
    classDef backendREST fill:#d4edda,stroke:#145a32
    classDef database fill:#fff3cd,stroke:#856404

    %% Click Events Frontend
    click FE_Entrypoint "https://github.com/visris-19/baatify/blob/main/Frontend/index.html"
    click FE_Entrypoint "https://github.com/visris-19/baatify/blob/main/Frontend/src/main.jsx"
    click FE_Entrypoint "https://github.com/visris-19/baatify/blob/main/Frontend/src/App.jsx"
    click AuthContext "https://github.com/visris-19/baatify/blob/main/Frontend/src/context/AuthContext.jsx"
    click SocketClient "https://github.com/visris-19/baatify/blob/main/Frontend/src/services/socket.js"
    click PrivateRoute "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/shared/PrivateRoute.jsx"
    click ChatHeader "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/chat/ChatHeader.jsx"
    click ChatList "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/chat/ChatList.jsx"
    click ChatWindow "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/chat/ChatWindow.jsx"
    click ChatInput "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/chat/ChatInput.jsx"
    click MessageBubble "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/chat/MessageBubble.jsx"
    click SearchUsers "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/chat/SearchUsers.jsx"
    click ChatPage "https://github.com/visris-19/baatify/blob/main/Frontend/src/pages/chat/Chat.jsx"
    click ProfileHeader "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/profile/ProfileHeader.jsx"
    click ProfileSettings "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/profile/ProfileSettings.jsx"
    click ProfilePage "https://github.com/visris-19/baatify/blob/main/Frontend/src/pages/profile/Profile.jsx"
    click StoryUploader "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/story/StoryUploader.jsx"
    click StoryViewer "https://github.com/visris-19/baatify/blob/main/Frontend/src/components/story/StoryViewer.jsx"
    click StoriesPage "https://github.com/visris-19/baatify/blob/main/Frontend/src/pages/story/Stories.jsx"
    click LoginPage "https://github.com/visris-19/baatify/blob/main/Frontend/src/pages/auth/Login.jsx"
    click RegisterPage "https://github.com/visris-19/baatify/blob/main/Frontend/src/pages/auth/Register.jsx"

    %% Click Events Backend
    click ServerEntry "https://github.com/visris-19/baatify/blob/main/Backend/index.js"
    click UserModel "https://github.com/visris-19/baatify/blob/main/Backend/models/User.js"
    click ChatModel "https://github.com/visris-19/baatify/blob/main/Backend/models/Chat.js"
    click MessageModel "https://github.com/visris-19/baatify/blob/main/Backend/models/Message.js"
    click StoryModel "https://github.com/visris-19/baatify/blob/main/Backend/models/Story.js"
    click AuthController "https://github.com/visris-19/baatify/blob/main/Backend/controllers/auth.controller.js"
    click ChatController "https://github.com/visris-19/baatify/blob/main/Backend/controllers/chat.controller.js"
    click MessageController "https://github.com/visris-19/baatify/blob/main/Backend/controllers/message.controller.js"
    click StoryController "https://github.com/visris-19/baatify/blob/main/Backend/controllers/story.controller.js"
    click AuthRoutes "https://github.com/visris-19/baatify/blob/main/Backend/routes/auth.routes.js"
    click ChatRoutes "https://github.com/visris-19/baatify/blob/main/Backend/routes/chat.routes.js"
    click MessageRoutes "https://github.com/visris-19/baatify/blob/main/Backend/routes/message.routes.js"
    click StoryRoutes "https://github.com/visris-19/baatify/blob/main/Backend/routes/story.routes.js"
    click UserRoutes "https://github.com/visris-19/baatify/blob/main/Backend/routes/user.routes.js"
    click AuthMiddleware "https://github.com/visris-19/baatify/blob/main/Backend/middleware/auth.js"
    click ErrorMiddleware "https://github.com/visris-19/baatify/blob/main/Backend/middleware/error.js"
    click UploadMiddleware "https://github.com/visris-19/baatify/blob/main/Backend/middleware/upload.js"
```
