# valma-julius/mesa_p2p architecture

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

- Interactive diagram: https://gitdiagram.com/valma-julius/mesa_p2p
- Repository: https://github.com/valma-julius/mesa_p2p

## Overview

##### Project Type and Purpose
– Mesa is a full-stack, peer-to-peer messaging application oriented toward complete user privacy.
– It consists of:
- A static front-end client (served by Nginx)
- A Ruby on Rails API & ActionCable server (“mesa”)
- A custom PeerJS signaling/forwarding server
- Supporting services: PostgreSQL database, a job queue (Redis), SSL termination

##### Main Components
a. Nginx Reverse Proxy (layer 7 gateway)
– Listens on ports 80/443, serves static assets, terminates TLS, and proxies requests.
b. Static Front-end Client
– Files under src/mesa_client/frontend (HTML/CSS/JS pages).
– Fetches REST API under `/api/*`.
– Connects to WebSocket channels for signaling.
– Uses PeerJS client library for P2P connectivity.
c. Rails Backend (“mesa”)
– REST API controllers (users, conversations, p2p_controller).
– ActionCable channels (p2p_chat_channel, signaling_channel, background_jobs_channel).
– Domain layer (service objects under app/domain/* for conversations and P2P logic).
– ActiveRecord models & migrations (users, conversations, messages, p2p_paths, p2p_transactions, jwt_denylist).
– Background jobs (app/jobs) for asynchronous tasks.
d. PeerJS Signaling/Forwarder Server
– Dockerfile + peerjs-config.json in src/backend/mesa.
– Listens on port 9000, handles WebRTC signaling and ICE candidate exchange.
e. Supporting Services
– PostgreSQL (data persistence).
– Redis (ActionCable pub/sub and job queue adapter).
– Docker Compose orchestrates Rails, DB, Redis, and PeerJS.

##### Relationships & Data Flows
– HTTP(S) traffic
- Browser → Nginx → Static files (front-end)
- Browser → Nginx → Rails API (`/api/*`) → PostgreSQL
– WebSocket traffic (ActionCable)
- Browser WebSocket → Nginx → Rails (p2p_chat & signaling channels) → Redis as pub/sub
– PeerJS traffic
- Browser PeerJS client → Nginx → PeerJS server (`/forwards`)
- PeerJS server coordinates ICE candidates and P2P paths
– Background Jobs
- Rails enqueues jobs → Redis → Worker threads process P2P notifications and forwarding

##### Architectural Patterns & Principles
– Layered architecture: Presentation (static client), API layer, domain/service layer, persistence layer.
– Service objects (app/domain) encapsulate business rules for P2P path finding, conversation management.
– Event-driven messaging: ActionCable channels and background jobs form asynchronous pipelines.
– Separation of concerns: Web server (Nginx) vs. application server (Rails) vs. signaling server (PeerJS).
– Security: JWT-based authentication, SSL termination, CORS policy, JWT deny list.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Browser
    subgraph "User Browser"
        direction TB
        UI["UI (HTML/CSS/JS)"]:::frontend
        RESTClient["REST Client"]:::frontend
        WSClient["WebSocket Client"]:::frontend
        P2PClient["PeerJS Client"]:::frontend
    end

    %% Nginx Proxy
    NGINX{{"Nginx Reverse Proxy"}}:::proxy

    %% Backend VPC
    subgraph "Backend VPC"
        direction TB
        DockerCompose["Docker Compose"]:::infrastructure
        subgraph "Rails API & ActionCable"
            direction TB
            RailsApp["Rails Backend (mesa)"]:::app
            Controllers["REST API Controllers"]:::app
            Channels["ActionCable Channels"]:::app
            Domain["Domain Layer (app/domain)"]:::app
            Models["ActiveRecord Models"]:::db
            Migrations["Migrations"]:::db
            Jobs["Background Jobs"]:::app
        end
        PeerJSServer["PeerJS Signaling Server"]:::p2p
        PostgreSQL["PostgreSQL"]:::db
        Redis["Redis"]:::db
    end

    %% Flows
    UI -->|HTTPS| NGINX
    RESTClient -->|HTTPS /api/*| NGINX
    WSClient -->|WSS /cable| NGINX
    P2PClient -->|HTTPS /forwards| NGINX

    NGINX -->|Proxy REST| RailsApp
    NGINX -->|Proxy WS| Controllers
    NGINX -->|Proxy Forwards| PeerJSServer

    RailsApp -->|uses Controllers| Controllers
    Controllers -->|invoke Domain Services| Domain
    Domain -->|read/write| Models
    Models -->|persist| PostgreSQL
    Controllers -->|enqueue| Jobs
    Jobs -->|jobs store| Redis
    Channels -->|pub/sub| Redis

    PeerJSServer -->|ICE signaling| P2PClient
    RailsApp -->|SQL| PostgreSQL
    RailsApp -->|Pub/Sub| Redis

    DockerCompose -->|orchestrates| RailsApp
    DockerCompose -->|orchestrates| PeerJSServer
    DockerCompose -->|orchestrates| PostgreSQL
    DockerCompose -->|orchestrates| Redis

    %% Click Events
    click UI "https://github.com/valma-julius/mesa_p2p/tree/main/src/mesa_client/frontend"
    click NGINX "https://github.com/valma-julius/mesa_p2p/blob/main/src/backend/mesa/docker-compose.yml"
    click RailsApp "https://github.com/valma-julius/mesa_p2p/tree/main/src/backend/mesa"
    click Controllers "https://github.com/valma-julius/mesa_p2p/tree/main/src/backend/mesa/app/controllers"
    click Channels "https://github.com/valma-julius/mesa_p2p/tree/main/src/backend/mesa/app/channels"
    click Domain "https://github.com/valma-julius/mesa_p2p/tree/main/src/backend/mesa/app/domain"
    click Models "https://github.com/valma-julius/mesa_p2p/tree/main/src/backend/mesa/app/models"
    click Migrations "https://github.com/valma-julius/mesa_p2p/tree/main/src/backend/mesa/db/migrate"
    click Jobs "https://github.com/valma-julius/mesa_p2p/tree/main/src/backend/mesa/app/jobs"
    click PeerJSServer "https://github.com/valma-julius/mesa_p2p/blob/main/src/backend/mesa/peerjs-config.json"
    click PeerJSServer "https://github.com/valma-julius/mesa_p2p/tree/main/src/backend/mesa/Dockerfile"
    click PostgreSQL "https://github.com/valma-julius/mesa_p2p/blob/main/src/backend/mesa/config/database.yml"
    click Redis "https://github.com/valma-julius/mesa_p2p/blob/main/src/backend/mesa/config/cable.yml"
    click DockerCompose "https://github.com/valma-julius/mesa_p2p/blob/main/src/backend/mesa/docker-compose.yml"

    %% Styles
    classDef frontend fill:#E0F7FA,stroke:#007B8A,color:#004D40;
    classDef proxy fill:#E8F5E9,stroke:#2E7D32,color:#1B5E20,shape:trapezoid;
    classDef app fill:#FFF3E0,stroke:#FB8C00,color:#E65100;
    classDef p2p fill:#FFE0B2,stroke:#FF6F00,color:#E65100;
    classDef db fill:#EFEBE9,stroke:#5D4037,color:#3E2723,shape:cylinder;
    classDef infrastructure fill:#ECEFF1,stroke:#607D8B,color:#263238;
```
