# burakorkmez/threads-clone architecture

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

- Interactive diagram: https://gitdiagram.com/burakorkmez/threads-clone
- Repository: https://github.com/burakorkmez/threads-clone

## Overview

##### Main Components
- Frontend: Located in the "frontend" directory, built with React (using Vite) and Chakra UI for styling. This layer handles UI interactions, routing between pages (AuthPage, HomePage, PostPage, ChatPage, etc.), state management through atoms and contexts, and integrates web socket communications.
- Backend: Under the "backend" directory, this is an Express.js-based REST API server. It houses:
- Controllers (messageController, postController, userController) handling request logic.
- Routes that expose endpoints (via messageRoutes, postRoutes, userRoutes).
- Models for MongoDB collections (conversationModel, messageModel, postModel, userModel).
- Middleware (e.g., protectRoute.js) for authentication and route protection.
- Cron jobs for scheduled tasks.
- Socket module (using Socket.IO) to enable real-time chat functionality.
- Database: Though not directly represented in the file tree, the README shows a MongoDB URI parameter in the .env file; hence a MongoDB database is in use.
- External Services: Includes Cloudinary for media/image handling as seen in the environment variables.

##### Relationships and Interactions
- The Frontend interacts with the Backend via HTTP requests for features like user authentication, posting, liking, commenting, following/unfollowing, and account management. Clearly mark these API calls.
- The Backend communicates with MongoDB using the connection module (connectDB.js) and interacts with various collections through its models.
- Integration with external services (Cloudinary for images) needs to be indicated.

##### Architectural Patterns & Design Principles
- The backend follows an MVC-like structure with separate directories for controllers, models, and routes.
- Authentication and authorization are handled via JWT tokens, as indicated by the utility functions and .env config.
- Use of middleware to protect routes emphasizes modular and layered architecture.
- For real-time functionalities, web sockets are integrated into this typical REST architecture, representing a hybrid model.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Frontend (React/Vite)"
        FE1["UI & Routing (pages)"]:::frontend
        FE2["State Management (atoms)"]:::frontend
        FE3["Assets (hooks)"]:::frontend
        FE4["Socket Communication (SocketContext)"]:::frontend
    end

    subgraph "Backend (Express.js API Server)"
        B1["API Controllers"]:::backend
        B2["API Routes"]:::backend
        B3["Database Models"]:::backend
        B4["Authentication Middleware"]:::backend
        B5["Cron Jobs"]:::backend
        B6["Real-Time Socket Module"]:::backend
        B7["Database Connection"]:::backend
        B8["Utility Functions"]:::backend
    end

    D["MongoDB Database"]:::database
    E["External Services (Cloudinary)"]:::external

    %% Frontend to Backend Communication
    FE1 -->|"HTTP_Request_Response"| B2
    FE4 <-->|"Socket_Message"| B6
    FE1 -->|"Media_Display"| E

    %% Backend Internal Relationships
    B2 -->|"Calls"| B1
    B1 -->|"Uses"| B3
    B4 -.->|"Protects"| B2
    B5 -->|"Schedules"| B1
    B7 -->|"Connects_to"| D
    B1 -.->|"Utilizes"| B8
    B1 -->|"Uploads_media"| E

    %% Click Events for Frontend
    click FE1 "https://github.com/burakorkmez/threads-clone/tree/master/frontend/src/pages"
    click FE2 "https://github.com/burakorkmez/threads-clone/tree/master/frontend/src/atoms"
    click FE3 "https://github.com/burakorkmez/threads-clone/tree/master/frontend/src/hooks"
    click FE4 "https://github.com/burakorkmez/threads-clone/blob/master/frontend/src/context/SocketContext.jsx"

    %% Click Events for Backend
    click B1 "https://github.com/burakorkmez/threads-clone/tree/master/backend/controllers"
    click B2 "https://github.com/burakorkmez/threads-clone/tree/master/backend/routes"
    click B3 "https://github.com/burakorkmez/threads-clone/tree/master/backend/models"
    click B4 "https://github.com/burakorkmez/threads-clone/blob/master/backend/middlewares/protectRoute.js"
    click B5 "https://github.com/burakorkmez/threads-clone/blob/master/backend/cron/cron.js"
    click B6 "https://github.com/burakorkmez/threads-clone/blob/master/backend/socket/socket.js"
    click B7 "https://github.com/burakorkmez/threads-clone/blob/master/backend/db/connectDB.js"
    click B8 "https://github.com/burakorkmez/threads-clone/blob/master/backend/utils/helpers/generateTokenAndSetCookie.js"

    %% Styles
    classDef frontend fill:#bbf,stroke:#333,stroke-width:2px;
    classDef backend fill:#fbb,stroke:#333,stroke-width:2px;
    classDef database fill:#cff,stroke:#333,stroke-width:2px;
    classDef external fill:#fcf,stroke:#333,stroke-width:2px;
```
