How tushar-prabhu/chat_app-springboot works
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.