# tosifan/chatapplication architecture

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

- Interactive diagram: https://gitdiagram.com/tosifan/chatapplication
- Repository: https://github.com/tosifan/chatapplication

## Overview

##### Project type and purpose
- This is a full-stack chat application. The frontend is a Flutter mobile app (lib/ and android/ directories). The backend is a Go-based REST and MQTT service (backend/ directory).
- Purpose: provide user authentication, direct messaging, group chats, media/file handling, real-time updates via MQTT, and local media caching.

##### File structure
- Top-level directories:
- android/ — native Android wrapper generated by Flutter
- backend/ — Go code for API, business logic, MQTT integration, and database connectivity
- lib/ — Dart/Flutter code: UI screens, models, providers (state management), and services
- test/ — Flutter widget tests
- Backend subdirectories:
- config/ — database connection setup
- controllers/ — HTTP endpoints for auth, user, message, group
- middleware/ — JWT authentication middleware
- models/ — domain models (User, Message, Group)
- mqtt/ — MQTT client for real-time publish/subscribe
- Frontend subdirectories:
- models/ — data classes generated via build_runner
- providers/ — state management for authentication and data fetching
- services/api/ — HTTP services (dio) for auth, direct and group messages, token management
- services/mqtt/ — real-time messaging service using MQTT
- services/cache/ — media caching service
- screens/ — UI organized by feature (auth, chat, group, home, profile, search, media viewers)

##### README
- The README is a Flutter template with no architecture details. We infer from code structure that it uses REST for CRUD operations and MQTT for real-time communication.

##### Components
a. Main components:
- Flutter Mobile Client
- Backend API Service (Go HTTP server)
- MQTT Broker (external or hosted service)
- Database (e.g., PostgreSQL, MySQL, or other RDBMS configured in backend/config/database.go)
- Media/File Storage (could be local disk or S3; implied by services/file)
- Local Cache on the client (media_cache_service)
b. Relationships and interactions:
- Mobile app → HTTP requests → Backend API (auth, fetch/send messages, group operations)
- Mobile app ↔ MQTT Broker → real-time messaging (publish/subscribe)
- Backend API ↔ Database → persist users, messages, groups
- Backend API ↔ MQTT Broker → publish incoming messages to topics; backend acts as publisher for server-triggered messages
- Mobile app ↔ Media/File Storage → upload and download images/videos via HTTP endpoints
- Mobile app ↔ Local Cache → store fetched media for offline access
c. Architectural patterns and principles:
- Frontend: Provider pattern for state management, MVVM-like separation (models, providers, views)
- Backend: MVC-inspired pattern (controllers, models), middleware for cross-cutting concerns (JWT auth)
- Use of asynchronous streams (MQTT) for real-time updates
- Separation of concerns: API layer vs. real-time layer vs. storage layer
d. Key technologies and libraries:
- Flutter / Dart: provider, dio (HTTP client), mqtt_client library
- Go: net/http or gin (for routing), gorilla/mux or similar, Paho MQTT client library
- Database driver (e.g., database/sql) configured in backend config

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Mobile Client Layer
    subgraph "Flutter Mobile Client" 
        direction TB
        MainApp["Main (lib/main.dart)"]:::frontend
        subgraph "Models"
            direction TB
            FMUser["User (lib/models/user.dart)"]:::frontend
            FMMessage["Message (lib/models/message.dart)"]:::frontend
            FMGroup["Group (lib/models/group.dart)"]:::frontend
        end
        subgraph "Providers"
            AuthProv["AuthProvider (lib/providers/auth_provider.dart)"]:::frontend
        end
        subgraph "Services - API"
            direction TB
            APIService["Dio API Service (lib/services/api/dio_api_service.dart)"]:::frontend
            AuthAPI["Auth API (lib/services/api/auth.dart)"]:::frontend
            DirectAPI["DirectMsg API (lib/services/api/directmessage.dart)"]:::frontend
            GroupAPI["GroupMsg API (lib/services/api/groupmessage.dart)"]:::frontend
            TokenAPI["Token API (lib/services/api/token.dart)"]:::frontend
        end
        subgraph "Services - MQTT"
            MQTTService["MQTT Service (lib/services/mqtt/mqtt_service.dart)"]:::frontend
        end
        subgraph "Services - Cache"
            CacheSvc["Media Cache (lib/services/cache/media_cache_service.dart)"]:::frontend
        end
        subgraph "Services - File"
            FileSvc["File Service (lib/services/file/file_service.dart)"]:::frontend
        end
        subgraph "Services - Network"
            NetSvc["Connectivity Service (lib/services/network/connectivity_service.dart)"]:::frontend
        end
        subgraph "Screens"
            direction TB
            AuthScreen["lib/screens/auth/"]:::frontend
            ChatScreen["lib/screens/chat/"]:::frontend
            GroupScreen["lib/screens/group/"]:::frontend
            HomeScreen["lib/screens/home/"]:::frontend
            ProfileScreen["lib/screens/profile/"]:::frontend
            SearchScreen["lib/screens/search/"]:::frontend
            ImgScreen["lib/screens/image/"]:::frontend
            VidScreen["lib/screens/video/"]:::frontend
            ForwardScreen["lib/screens/forward/"]:::frontend
        end
    end

    %% Android wrapper
    AndroidWrapper["Android Wrapper (android/)"]:::frontend

    %% Backend Layer
    subgraph "Backend API Server" 
        direction TB
        MainGo["main.go (backend/main.go)"]:::backend
        ConfigDB["Database Config (backend/config/database.go)"]:::backend
        subgraph "Controllers"
            AuthCtrl["AuthController (backend/controllers/auth_controller.go)"]:::backend
            UserCtrl["UserController (backend/controllers/user_controller.go)"]:::backend
            MsgCtrl["MessageController (backend/controllers/message_controller.go)"]:::backend
            GroupCtrl["GroupController (backend/controllers/group_controller.go)"]:::backend
        end
        Middleware["JWT Middleware (backend/middleware/auth.go)"]:::backend
        ModelsGo["Models (backend/models/models.go)"]:::backend
        MQTTClient["MQTT Client (backend/mqtt/client.go)"]:::backend
        GoMod["go.mod/go.sum (backend/go.mod, backend/go.sum)"]:::backend
    end

    %% Infrastructure
    subgraph "Infrastructure"
        direction TB
        DB["Database"]:::infra
        MQTTBroker["MQTT Broker"]:::infra
        FileStorage["File Storage"]:::infra
    end

    %% Relationships
    MainApp -->|"uses Providers"| AuthProv
    MainApp -->|"loads Models"| FMUser
    MainApp --> FMMessage
    MainApp --> FMGroup
    MainApp -->|"navigates to"| AuthScreen
    MainApp --> HomeScreen

    AuthProv -->|"calls"| APIService
    APIService -->|"HTTP JSON/REST"| MainGo
    APIService --> FileStorage
    MQTTService <-->|"MQTT Pub/Sub"| MQTTBroker
    MainGo -->|"SQL Queries"| DB
    MainGo -->|"MQTT Publish"| MQTTBroker
    FileSvc --> FileStorage
    CacheSvc --> FileStorage
    NetSvc -->|"network status"| MainApp

    MainGo --> AuthCtrl
    MainGo --> UserCtrl
    MainGo --> MsgCtrl
    MainGo --> GroupCtrl
    AuthCtrl --> ModelsGo
    MsgCtrl --> ModelsGo
    GroupCtrl --> ModelsGo
    MainGo --> ConfigDB
    MainGo --> Middleware
    MainGo --> MQTTClient

    %% Click events
    click MainApp "https://github.com/tosifan/chatapplication/blob/main/lib/main.dart"
    click AndroidWrapper "https://github.com/tosifan/chatapplication/tree/main/android/"
    click FMUser "https://github.com/tosifan/chatapplication/blob/main/lib/models/user.dart"
    click FMMessage "https://github.com/tosifan/chatapplication/blob/main/lib/models/message.dart"
    click FMGroup "https://github.com/tosifan/chatapplication/blob/main/lib/models/group.dart"
    click AuthProv "https://github.com/tosifan/chatapplication/blob/main/lib/providers/auth_provider.dart"
    click APIService "https://github.com/tosifan/chatapplication/blob/main/lib/services/api/dio_api_service.dart"
    click AuthAPI "https://github.com/tosifan/chatapplication/blob/main/lib/services/api/auth.dart"
    click DirectAPI "https://github.com/tosifan/chatapplication/blob/main/lib/services/api/directmessage.dart"
    click GroupAPI "https://github.com/tosifan/chatapplication/blob/main/lib/services/api/groupmessage.dart"
    click TokenAPI "https://github.com/tosifan/chatapplication/blob/main/lib/services/api/token.dart"
    click MQTTService "https://github.com/tosifan/chatapplication/blob/main/lib/services/mqtt/mqtt_service.dart"
    click CacheSvc "https://github.com/tosifan/chatapplication/blob/main/lib/services/cache/media_cache_service.dart"
    click FileSvc "https://github.com/tosifan/chatapplication/blob/main/lib/services/file/file_service.dart"
    click NetSvc "https://github.com/tosifan/chatapplication/blob/main/lib/services/network/connectivity_service.dart"
    click AuthScreen "https://github.com/tosifan/chatapplication/tree/main/lib/screens/auth/"
    click ChatScreen "https://github.com/tosifan/chatapplication/tree/main/lib/screens/chat/"
    click GroupScreen "https://github.com/tosifan/chatapplication/tree/main/lib/screens/group/"
    click HomeScreen "https://github.com/tosifan/chatapplication/tree/main/lib/screens/home/"
    click ProfileScreen "https://github.com/tosifan/chatapplication/tree/main/lib/screens/profile/"
    click SearchScreen "https://github.com/tosifan/chatapplication/tree/main/lib/screens/search/"
    click ImgScreen "https://github.com/tosifan/chatapplication/tree/main/lib/screens/image/"
    click VidScreen "https://github.com/tosifan/chatapplication/tree/main/lib/screens/video/"
    click ForwardScreen "https://github.com/tosifan/chatapplication/tree/main/lib/screens/forward/"
    click MainGo "https://github.com/tosifan/chatapplication/blob/main/backend/main.go"
    click ConfigDB "https://github.com/tosifan/chatapplication/blob/main/backend/config/database.go"
    click AuthCtrl "https://github.com/tosifan/chatapplication/blob/main/backend/controllers/auth_controller.go"
    click UserCtrl "https://github.com/tosifan/chatapplication/blob/main/backend/controllers/user_controller.go"
    click MsgCtrl "https://github.com/tosifan/chatapplication/blob/main/backend/controllers/message_controller.go"
    click GroupCtrl "https://github.com/tosifan/chatapplication/blob/main/backend/controllers/group_controller.go"
    click Middleware "https://github.com/tosifan/chatapplication/blob/main/backend/middleware/auth.go"
    click MQTTClient "https://github.com/tosifan/chatapplication/blob/main/backend/mqtt/client.go"
    click ModelsGo "https://github.com/tosifan/chatapplication/blob/main/backend/models/models.go"
    click GoMod "https://github.com/tosifan/chatapplication/blob/main/backend/go.mod"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#005F9E,color:#000
    classDef backend fill:#DFF2D8,stroke:#4F8A10,color:#000
    classDef infra fill:#EEEEEE,stroke:#999999,color:#000
```
