# tushar-prabhu/chat_app-springboot architecture

> Architecture diagram of the GitHub repository tushar-prabhu/chat_app-springboot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tushar-prabhu/chat_app-springboot
- Repository: https://github.com/tushar-prabhu/chat_app-springboot

## Overview

##### Main Components
- Client/User Interface: Represented by the static resources (HTML, CSS, JS) under the “src/main/resources/static” (and similarly in the “bin” folder). This includes the login form (for username and password), chat page, and JavaScript logic that sets up the WebSocket connection.
- WebSocket Communication Layer: Illustrate the client establishing a connection via SockJS to the WebSocket endpoint “/websocket”. Note that the client uses STOMP over this connection.
- WebSocket Configuration & Message Broker: On the server side, the configuration classes (WebSocketConfig.java) set up the endpoints and configure the in-memory message broker with the “/topic” (for broadcasting) and “/app” prefix (for client-to-server messaging).
- Controller & Message Handling: The ChatController.java component defines the endpoints ("/chat.register" and "/chat.send") that listen for incoming messages and use annotations (e.g., @MessageMapping and @SendTo) to process and broadcast messages.
- Event Listeners: The WebSocketEventListener.java listens for disconnect events so that when a user leaves, a leave message is sent to all subscribers.
- Data Model: The ChatMessage.java class defines the payload (with content, sender, and message type) that is passed between clients and server.

##### Relationships and Interactions
- Signup flow where upon entering a username and password (validated in the UI), the client subscribes to “/topic/public” and sends messages to “/app/chat.register” (for joining) or “/app/chat.send” (for a chat message).
- Server’s WebSocketConfig component which registers the STOMP endpoint and designates the simple broker prefixes.
- That the ChatController receives messages on “/app/chat.register” and “/app/chat.send” and then forwards responses (broadcasts) to “/topic/public.”
- That the WebSocketEventListener intercepts disconnect events and sends a “LEAVE” type message to the broker.

##### Architectural Patterns & Design Principles
- Separation of Concerns: The frontend (UI) is completely separated from the backend logic. The Spring Boot application handles configuration, controllers, and messaging independently.
- Event-driven Communication: Using WebSockets (with STOMP) enables a publish/subscribe pattern for real-time messaging.
- In-memory Message Broker: Emphasize the use of a simple in-memory broker for routing messages from controllers to subscribed clients.
- Fallback Strategy: The integration of SockJS shows an important design decision to support browsers that might not support native WebSockets.

##### Relevant Technologies and Frameworks
- Spring Boot for the backend application.
- Spring WebSocket (with STOMP and SockJS) for managing real-time connections.
- HTML/CSS/JavaScript for the frontend, with the client JS using libraries to manage WebSocket connections and UI interactions.
- Lombok (on the Java side) for reducing boilerplate in the model classes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Side"
        UI["Client UI (HTML/CSS)"]:::frontend
        WSComm["WebSocket Communication (JS)"]:::frontend
    end

    subgraph "Server Side (Spring Boot)"
        WSC["WebSocketConfig"]:::backend
        Controller["ChatController"]:::backend
        WSListener["WebSocketEventListener"]:::backend
        Broker["Message Broker (/topic/public)"]:::backend
        Model["ChatMessage Model"]:::model
    end

    UI -->|"LoadsResources"| WSComm
    WSComm -->|"ConnectsViaSockJS/STOMP_on_/websocket"| WSC
    WSComm -->|"Sends_/app/chat.register_/_app/chat.send"_| Controller
    Controller -->|"BroadcastsMessages"| Broker
    WSListener -->|"Sends_LEAVE_Message"| Broker
    Broker -->|"Broadcasts_to_Clients"| WSComm

    click UI "https://github.com/tushar-prabhu/chat_app-springboot/tree/main/src/main/resources/static"
    click WSComm "https://github.com/tushar-prabhu/chat_app-springboot/blob/main/src/main/resources/static/js/main.js"
    click WSC "https://github.com/tushar-prabhu/chat_app-springboot/blob/main/src/main/java/in/tusharprabhu/chatapp/config/WebSocketConfig.java"
    click Controller "https://github.com/tushar-prabhu/chat_app-springboot/blob/main/src/main/java/in/tusharprabhu/chatapp/controller/ChatController.java"
    click WSListener "https://github.com/tushar-prabhu/chat_app-springboot/blob/main/src/main/java/in/tusharprabhu/chatapp/config/WebSocketEventListener.java"
    click Model "https://github.com/tushar-prabhu/chat_app-springboot/blob/main/src/main/java/in/tusharprabhu/chatapp/model/ChatMessage.java"

    classDef frontend fill:#add8e6,stroke:#333,stroke-width:1px;
    classDef backend fill:#90ee90,stroke:#333,stroke-width:1px;
    classDef model fill:#ffffe0,stroke:#333,stroke-width:1px;
```
