# marioperezhurtado/boble2 architecture

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

- Interactive diagram: https://gitdiagram.com/marioperezhurtado/boble2
- Repository: https://github.com/marioperezhurtado/boble2

## Overview

##### Core Components
- Frontend Client: Built with Svelte/SvelteKit and TailwindCSS. This layer includes all UI components (the “ui” and “routes” folders) and user interaction elements.
- API Layer (tRPC): The communication bridge between the frontend and backend, defined in the “src/lib/trpc” folder. This layer handles routes for authentication, chat, contacts, messages, and more.
- Backend Server: Powered by SvelteKit (with a custom Node server defined in server.js) that orchestrates API requests, WebSocket connections, and server-side logic.
- WebSocket Service: Implemented using Socket.io (located in “src/lib/socket”), this is responsible for providing real-time, bidirectional communication.
- Database Layer: PostgreSQL is used as the primary data store with DrizzleORM handling the data models and queries (in “src/lib/db”). The schema, migrations, and specific query functions (for chats, contacts, messages, etc.) are clearly separated.
- Authentication & Encryption: Authentication is implemented using Lucia (in “src/lib/auth”), and end-to-end encryption is handled via the Web Crypto API (see “src/lib/utils/encryption.ts”) with ECDH and AES-GCM specifics.
- Email and File Upload Modules: Email functionality is built using Nodemailer and Svelte-email (in “src/lib/email”), while file uploads (using Amazon S3 SDK) reside in “src/lib/file-upload”, with a fake S3 server provided in the “s3” directory for development.
- External Services: These include SendGrid for SMTP, Giphy for media support, and AWS S3 (or a development S3 server) for file storage.

##### Relationships and Interactions
- The Frontend interacts with the API Layer via tRPC procedures which then call back-end functions.
- For persistent data, the Backend communicates with the PostgreSQL database using DrizzleORM.
- Authentication flows (account creation, login, email verifications, password resets), as well as encryption key generation and crypto routines, tie into both the API Layer and a secure local storage mechanism on the client side.
- File uploads and email triggers represent integrations with external APIs/services, and should be clearly connected to the backend.

##### Architectural Patterns and Design Principles
- Modularization: Different aspects of the application are separated into dedicated directories (e.g., “auth”, “db”, “socket”, “email”, “file-upload”).
- API Layer Separation: tRPC router processes make the backend procedures strongly typed and ensure clear boundaries between client and server logic.
- Real-Time Communication: The architecture supports WebSocket communication integrated into the SvelteKit custom server.
- Security Constructs: Notice the end-to-end encryption strategy that uses the Diffie-Hellman key exchange (ECDH) and AES-GCM encryption methodology.

##### Specific Technologies & Frameworks
- Svelte/SvelteKit, TailwindCSS (Frontend)
- tRPC, Node.js custom server (Backend)
- Socket.io (WebSocket)
- PostgreSQL and DrizzleORM (Database)
- Lucia (Authentication)
- Nodemailer and Svelte-email (Email service)
- AWS S3 SDK (File storage)
- External API integrations like SendGrid and Giphy.

##### Specific Elements
- Labeled components for each primary module: Frontend, API/tRPC, Backend Server, WebSocket Service, Database, Authentication, Email Service, File Upload Service, External APIs.
- Blue for frontend components,
- Green for backend processes,
- Gray for the database,
- Orange for external services, and
- Labels to indicate critical frameworks and technologies (Svelte, tRPC, PostgreSQL, Socket.io, etc.).

##### Putting It All Together
- Lastly, include testing and configuration aspects (such as the testing folder, and relevant config files) as supporting infrastructure elements.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "Frontend Client"
        A1["UI Components"]:::frontend
        A2["Routes"]:::frontend
    end

    %% API Layer Subgraph
    subgraph "API Layer (tRPC)"
        B1["tRPC Client & Shared"]:::trpc
        B2["tRPC Server Endpoints"]:::trpc
    end

    %% Backend Services Subgraph
    subgraph "Backend Services"
        C1["SvelteKit Custom Server"]:::backend
        C2["Server Hooks"]:::backend
        C3["Socket.io Service"]:::backend
        C4["Email Module"]:::backend
        C5["File Upload Module"]:::backend
        C6["Lucia Auth"]:::security
        C7["Encryption"]:::security
    end

    %% Database Node
    D["PostgreSQL / DrizzleORM Database"]:::database

    %% External Integrations Subgraph
    subgraph "External Integrations"
        E1["SendGrid (SMTP)"]:::external
        E2["Giphy Media Service"]:::external
        E3["AWS S3 / Fake S3"]:::external
    end

    %% Connections between components
    A1 -->|"trpc-call"| B1
    A2 -->|"trpc-call"| B1

    B1 -->|"requests"| B2
    B2 -->|"orchestrates"| C1
    
    C1 -->|"data queries"| D
    C1 -->|"auth flows"| C6
    C1 -->|"encryption ops"| C7
    C1 -->|"handles emails"| C4
    C1 -->|"file uploads"| C5
    C1 -- "ws-connection" --- C3

    %% Bidirectional connection for real-time updates
    A1 <==>|"realTime"| C3

    %% External integrations from backend modules
    C4 -->|"sends email"| E1
    C5 -->|"uploads files"| E3
    C1 -.->|"media fetch"| E2

    %% Click Events
    click A1 "https://github.com/marioperezhurtado/boble2/tree/main/src/lib/ui"
    click A2 "https://github.com/marioperezhurtado/boble2/tree/main/src/routes"
    click B1 "https://github.com/marioperezhurtado/boble2/blob/main/src/lib/trpc/client.ts"
    click B1 "https://github.com/marioperezhurtado/boble2/blob/main/src/lib/trpc/shared.ts"
    click B2 "https://github.com/marioperezhurtado/boble2/tree/main/src/lib/trpc/server"
    click C1 "https://github.com/marioperezhurtado/boble2/blob/main/server.js"
    click C2 "https://github.com/marioperezhurtado/boble2/blob/main/src/hooks.server.ts"
    click C3 "https://github.com/marioperezhurtado/boble2/tree/main/src/lib/socket"
    click D "https://github.com/marioperezhurtado/boble2/tree/main/src/lib/db"
    click C6 "https://github.com/marioperezhurtado/boble2/blob/main/src/lib/auth/auth.ts"
    click C7 "https://github.com/marioperezhurtado/boble2/blob/main/src/lib/utils/encryption.ts"
    click C4 "https://github.com/marioperezhurtado/boble2/tree/main/src/lib/email"
    click C5 "https://github.com/marioperezhurtado/boble2/tree/main/src/lib/file-upload"
    click E3 "https://github.com/marioperezhurtado/boble2/tree/main/s3"

    %% Styles
    classDef frontend fill:#add8e6,stroke:#00008b,stroke-width:2px;
    classDef trpc fill:#b0e0e6,stroke:#5f9ea0,stroke-width:2px;
    classDef backend fill:#90ee90,stroke:#006400,stroke-width:2px;
    classDef database fill:#d3d3d3,stroke:#696969,stroke-width:2px;
    classDef security fill:#ffcccb,stroke:#cd5c5c,stroke-width:2px;
    classDef external fill:#ffdab9,stroke:#ff8c00,stroke-width:2px;
```
