# gitcoder052023/streamnet architecture

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

- Interactive diagram: https://gitdiagram.com/gitcoder052023/streamnet
- Repository: https://github.com/gitcoder052023/streamnet

## Overview

##### Project Type and Purpose
- StreamNet is a full-stack, microservices-based real-time chat application with built-in AI support.
- It comprises multiple Node.js services (Engine, Main Server, ChatLLama frontend & backend) plus static frontend assets, all collaborating over HTTPS/WebSockets and MongoDB.

##### Key Top-Level Directories
- ChatLLama/ ← AI service (separate frontend & backend)
- src/Engine/ ← Core Engine service handling WebSocket chat and HTTP
- Server/ ← Main HTTPS server (REST API, auth, static file serving)
- Public/ & Routes/ ← Static UI templates, CSS/JS, and routing helpers
- Shared config files (.env, SSL, ESLint, Tailwind)

##### Main Components
a. Client (Browser)
- Static UI (HTML/CSS/JS) served from the Main Server & Public folder
- ChatLLama UI served on port 3001
b. Engine Service (src/Engine) – Port 3000
- HTTP: health checks, environment config, rate limiting, CORS
- WebSocket: real-time chat socket (socket.io)
- Auth utils (JWT issuance, HMAC signing)
c. Main Server (Server/) – Port 4000 (HTTPS)
- Express routes: /auth, /conversations, /models
- Controllers & middleware (authMiddleware, validation, emailService)
- Static file serving (Public & Templates)
- WebSocket chat socket fallback or REST API for message history
d. ChatLLama Frontend – Port 3001
- Interactive AI chat UI (model selection, streaming controls)
- Communicates over WebSocket to ChatLLama Backend
e. ChatLLama Backend – Port 5000
- WebSocket API forwarding to local Ollama models
- MongoDB storage of AI conversations (ChatLLama database)
f. Databases
- MongoDB “StreamNet” – Users, Messages, Conversations, otps
- MongoDB “ChatLLama” – users, conversations
g. External Dependencies
- Gmail SMTP for OTP emails
- Local Ollama daemon (AI models)

##### Relationships & Data Flows
- Browser → HTTPS REST (Server: auth, conversation list)
- Browser ↔ WebSocket (Engine: real-time chat)
- Browser ↔ WebSocket (ChatLLama backend: AI streaming)
- Engine & Server → MongoDB StreamNet (CRUD)
- ChatLLama backend → MongoDB ChatLLama
- ChatLLama backend → Ollama daemon (AI inference)
- Server → Gmail SMTP (OTP delivery)

##### Architectural Patterns & Principles
- Microservices: independent Engine, Main Server, AI service
- Event-driven / Pub-sub: Socket.io for low-latency messaging
- Layered MVC in Main Server (routes → controllers → models)
- Security: SSL/TLS at every service, JWT & HMAC, input sanitization, rate limiting
- Theming & Responsive UI: Tailwind CSS, modular JS components

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    Browser["Client Browser"]:::frontend

    subgraph "Client UI"
        StaticAssets["Static UI Assets (Public/)"]:::frontend
        RouteHelpers["Frontend Route Helpers (Routes/)"]:::frontend
        ChatFrontend["ChatLLama Frontend (Port 3001)"]:::frontend
    end

    %% Backend Services
    subgraph "Backend Services"
        EngineSvc["Engine Service (Port 3000)"]:::backend
        MainServer["Main HTTPS Server (Port 4000)"]:::backend
        ChatBackend["ChatLLama Backend (Port 5000)"]:::backend
    end

    %% Databases
    subgraph "Databases"
        StreamDB["MongoDB StreamNet"]:::database
        LlamaDB["MongoDB ChatLLama"]:::database
    end

    %% External
    subgraph "External Dependencies"
        SMTP["Gmail SMTP"]:::external
        Ollama["Local Ollama Daemon"]:::external
    end

    %% Shared Config
    subgraph "Shared Config Files"
        Env[".env.example"]:::config
        TailwindConfig["tailwind.config.js"]:::config
        TailwindCSS["src/tailwind.css"]:::config
    end

    %% Connections
    Browser -->|"HTTPS REST"| MainServer
    Browser -->|"WebSocket wss://"| EngineSvc
    Browser -->|"WebSocket wss://"| ChatBackend
    MainServer -->|"Serves Static UI"| Browser
    MainServer -->|"MongoDB URI"| StreamDB
    EngineSvc -->|"MongoDB URI"| StreamDB
    ChatBackend -->|"MongoDB URI"| LlamaDB
    ChatBackend -->|"Ollama API"| Ollama
    MainServer -->|"SMTP"| SMTP

    StaticAssets --> Browser
    RouteHelpers --> MainServer

    %% Click Events
    click ChatFrontend "https://github.com/gitcoder052023/streamnet/tree/main/ChatLLama/public/"
    click ChatBackend "https://github.com/gitcoder052023/streamnet/tree/main/ChatLLama/server/"
    click EngineSvc "https://github.com/gitcoder052023/streamnet/tree/main/src/Engine/"
    click MainServer "https://github.com/gitcoder052023/streamnet/tree/main/Server/"
    click StaticAssets "https://github.com/gitcoder052023/streamnet/tree/main/Public/"
    click RouteHelpers "https://github.com/gitcoder052023/streamnet/tree/main/Routes/"
    click Env "https://github.com/gitcoder052023/streamnet/blob/main/.env.example"
    click TailwindConfig "https://github.com/gitcoder052023/streamnet/blob/main/tailwind.config.js"
    click TailwindCSS "https://github.com/gitcoder052023/streamnet/blob/main/src/tailwind.css"

    %% Styles
    classDef frontend fill:#add8e6,stroke:#000,stroke-width:1px
    classDef backend fill:#90ee90,stroke:#000,stroke-width:1px
    classDef database fill:#ffa500,stroke:#000,stroke-width:1px
    classDef external fill:#d3d3d3,stroke:#000,stroke-width:1px
    classDef config fill:#fffacd,stroke:#000,stroke-width:1px
```
