# scarllet-hash/chat_hub architecture

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

- Interactive diagram: https://gitdiagram.com/scarllet-hash/chat_hub
- Repository: https://github.com/scarllet-hash/chat_hub

## Overview

##### Project Type and Purpose
- ChatHub is a full-stack, real-time multi-user chat application.
- Frontend: React app running in the browser, communicating over WebSocket.
- Backend: Native C TCP server (`chat_server.c`) handling chat logic in memory.
- Bridge: `websocketd` + `socat` acting as a WebSocket-to-TCP proxy.
- Containerized via Docker and orchestrated by `docker-compose`.

##### File-structure Analysis
- Top-level:
- `client/` (React code + Dockerfile)
- `server/` (C server + Dockerfile)
- `websocketd/` (proxy scripts + Dockerfile)
- `docker-compose.yml`
- No database directory: state is in-memory in the C server.
- Presence of shell script (`tcp-proxy.sh`) and `websocketd` config indicates a 3-tier pipeline.

##### Key Architecture Insights from README
- Clients connect via WebSocket to `websocketd` (port 8080).
- `websocketd` invokes `tcp-proxy.sh`, which uses `socat` to open a TCP socket to the C server (port 12345).
- The C server handles all chat features (private, group, online list, notifications) over plain TCP.

##### Main Components
a. React Client (Browser)
- Runs on port 3000 (dev) or 80/8080 (Docker)
b. WebSocket Bridge
- Inside: `tcp-proxy.sh` → `socat`
- Exposes ws://localhost:8080
c. Chat Server
- Listens on TCP port 12345
- In-memory user/session management
d. Orchestration Layer
- Docker Compose service definitions for client, websocketd, server
- Network overlay: each container in the same Docker network

##### Architectural Patterns & Design Principles
- Separation of Concerns: UI (React) vs. Chat Logic (C) vs. Transport (websocketd+socat).
- Micro-service-like proxy layer to avoid adding WebSocket code to the legacy C server.
- Containerization: decoupled services with explicit network links.

##### Detail Checklist
- [✓] React client entry point (`App.js`, WebSocket connection)
- [✓] websocketd container invoking `tcp-proxy.sh`
- [✓] socat TCP bridge
- [✓] C server listening on POSIX sockets
- [✓] Docker Compose service names & links
- [✓] Data-flow directions and protocols

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend Layer"
        Browser["React Frontend<br/>(Port 3000/80)"]:::frontend
    end

    subgraph "Transport Layer"
        WS["websocketd<br/>(ws://:8080)"]:::proxy
        ProxyScript["tcp-proxy.sh"]:::proxy
        Socat["socat"]:::proxy
    end

    subgraph "Server Layer"
        ChatServer["chat_server (C/TCP)<br/>TCP:12345"]:::server
    end

    subgraph "Orchestration Layer"
        Compose["Docker Compose"]:::orchestration
    end

    Browser -->|"WebSocket ws://8080"| WS
    WS -->|"STDIO"| ProxyScript
    ProxyScript -->|"STDIO"| Socat
    Socat -->|"TCP 12345"| ChatServer
    ChatServer -->|"TCP 12345"| Socat
    Socat -->|"STDIO"| ProxyScript
    ProxyScript -->|"STDIO"| WS
    WS -->|"WebSocket ws://8080"| Browser

    click Browser "https://github.com/scarllet-hash/chat_hub/blob/main/client/package.json"
    click Browser "https://github.com/scarllet-hash/chat_hub/tree/main/client/Dockerfile"
    click Browser "https://github.com/scarllet-hash/chat_hub/blob/main/client/public/index.html"
    click Browser "https://github.com/scarllet-hash/chat_hub/blob/main/client/src/App.js"
    click Browser "https://github.com/scarllet-hash/chat_hub/blob/main/client/src/index.js"
    click WS "https://github.com/scarllet-hash/chat_hub/tree/main/websocketd/Dockerfile"
    click ProxyScript "https://github.com/scarllet-hash/chat_hub/blob/main/websocketd/tcp-proxy.sh"
    click ChatServer "https://github.com/scarllet-hash/chat_hub/blob/main/server/chat_server.c"
    click ChatServer "https://github.com/scarllet-hash/chat_hub/tree/main/server/Dockerfile"
    click Compose "https://github.com/scarllet-hash/chat_hub/blob/main/docker-compose.yml"

    classDef frontend fill:#D0E6FF,stroke:#004C99,color:#00264D
    classDef proxy fill:#FFE5CC,stroke:#CC6600,color:#663300
    classDef server fill:#E6FFE6,stroke:#339933,color:#006600
    classDef orchestration fill:#F2F2F2,stroke:#666666,color:#333333
```
