# dhij/go-next-ts_chat architecture

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

- Interactive diagram: https://gitdiagram.com/dhij/go-next-ts_chat
- Repository: https://github.com/dhij/go-next-ts_chat

## Overview

##### Project type and purpose
- This is a full-stack, real-time chat application.
- Frontend: Next.js + TypeScript + Tailwind CSS.
- Backend: Go microservice providing both REST endpoints (user auth/management) and WebSocket endpoints for real-time messaging.
- Database: SQL (users table via migrations).

##### File structure
Top-level directories
- client/ – Next.js app
– pages/ (UI routes: login, chat app)
– modules/ (AuthProvider, WebSocketProvider)
– components/ (ChatBody UI)
– constants/, styles/, public/
- server/ – Go service
– cmd/main.go (entry point)
– router/router.go (defines HTTP & WS routes)
– internal/user (user_repository, service, handler)
– internal/ws (hub.go, client.go, ws_handler.go)
– db (db.go, migrations)
– util/password.go
– Makefile, go.mod/sum

##### README for additional insights
- Describes “hub” architecture: a central goroutine managing rooms, channels (Register, Unregister, Broadcast).
- Sequence: client connects → ws/joinRoom → hub registers client → client.readMessage → hub.Broadcast → hub distributes → client.writeMessage → UI.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Lane
    subgraph "Frontend (Next.js Client)"
        direction TB
        Browser["Web Browser / React UI"]:::ui
        LoginPage["Login Page"]:::ui
        ChatPage["Chat Application Page"]:::ui
        AuthProvider["AuthProvider Module"]:::control
        WebSocketProvider["WebSocketProvider Module"]:::control
        ChatBody["ChatBody Component"]:::ui
    end

    %% Backend Lane
    subgraph "Backend (Go Service + DB)"
        direction TB
        Main["Backend Entry Point (main.go)"]:::control
        Router["HTTP & WS Router"]:::control
        UserHandler["User Handler (user_handler.go)"]:::control
        UserService["User Service (user_service.go)"]:::control
        UserRepo["User Repository (user_repository.go)"]:::data
        DBConn["Database Connection (db.go)"]:::data
        Migrations["SQL Migrations"]:::data
        PasswordUtil["Password Utility"]:::data
        WSHandler["WebSocket Handler (ws_handler.go)"]:::realtime
        Hub["WebSocket Hub (hub.go)"]:::realtime
        ClientConn["WebSocket Client (client.go)"]:::realtime
    end

    %% Frontend Flow
    Browser -->|navigates to| LoginPage
    LoginPage -->|auth flows| AuthProvider
    AuthProvider -->|REST API| Router
    Browser -->|after login| ChatPage
    ChatPage -->|renders| AuthProvider
    ChatPage -->|uses| WebSocketProvider
    WebSocketProvider -->|opens WS| WSHandler
    ChatPage -->|renders| ChatBody
    ChatBody -->|sends/receives| WebSocketProvider

    %% Backend Flow
    Main --> Router
    Router -->|HTTP /signup,/login| UserHandler
    UserHandler --> UserService
    UserService --> PasswordUtil
    UserService --> UserRepo
    UserRepo --> DBConn
    DBConn --> Migrations
    Router -->|WS /ws/joinRoom| WSHandler
    WSHandler --> Hub
    WSHandler --> ClientConn
    ClientConn -->|readMessage| Hub
    Hub -->|Broadcast| ClientConn
    ClientConn --> WebSocketProvider

    %% Click Events
    click Browser "https://github.com/dhij/go-next-ts_chat/tree/main/client/"
    click LoginPage "https://github.com/dhij/go-next-ts_chat/blob/main/client/pages/login/index.tsx"
    click ChatPage "https://github.com/dhij/go-next-ts_chat/blob/main/client/pages/app/index.tsx"
    click AuthProvider "https://github.com/dhij/go-next-ts_chat/blob/main/client/modules/auth_provider.tsx"
    click WebSocketProvider "https://github.com/dhij/go-next-ts_chat/blob/main/client/modules/websocket_provider.tsx"
    click ChatBody "https://github.com/dhij/go-next-ts_chat/blob/main/client/components/chat_body.tsx"
    click Router "https://github.com/dhij/go-next-ts_chat/blob/main/server/router/router.go"
    click WSHandler "https://github.com/dhij/go-next-ts_chat/blob/main/server/internal/ws/ws_handler.go"
    click Hub "https://github.com/dhij/go-next-ts_chat/blob/main/server/internal/ws/hub.go"
    click ClientConn "https://github.com/dhij/go-next-ts_chat/blob/main/server/internal/ws/client.go"
    click UserHandler "https://github.com/dhij/go-next-ts_chat/blob/main/server/internal/user/user_handler.go"
    click UserService "https://github.com/dhij/go-next-ts_chat/blob/main/server/internal/user/user_service.go"
    click UserRepo "https://github.com/dhij/go-next-ts_chat/blob/main/server/internal/user/user_repository.go"
    click DBConn "https://github.com/dhij/go-next-ts_chat/blob/main/server/db/db.go"
    click Migrations "https://github.com/dhij/go-next-ts_chat/blob/main/server/db/migrations/20221207050938_add_users_table.up.sql"
    click PasswordUtil "https://github.com/dhij/go-next-ts_chat/blob/main/server/util/password.go"
    click Main "https://github.com/dhij/go-next-ts_chat/blob/main/server/cmd/main.go"
    click GoMod "https://github.com/dhij/go-next-ts_chat/blob/main/server/go.mod"
    click GoSum "https://github.com/dhij/go-next-ts_chat/blob/main/server/go.sum"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#0366D6,color:#0366D6
    classDef control fill:#DFF5D4,stroke:#2E8B57,color:#2E8B57
    classDef data fill:#FFE4B3,stroke:#DAA520,color:#DAA520
    classDef realtime fill:#E9D0FF,stroke:#8A2BE2,color:#8A2BE2
    class GoMod,GoSum data
```
