Skip to content

YoungDev7/chatapp-react-frontend

Repository files navigation

Chat Application - Frontend

A real-time chat application frontend built with React, TypeScript, Vite, and Redux Toolkit. Features JWT authentication, WebSocket connections for real-time messaging, and a responsive Material-UI design.

Application Architecture

πŸš€ Technologies Used

  • React 19
  • TypeScript
  • Vite
  • Redux Toolkit
  • Material-UI (MUI)
  • STOMP/SockJS

πŸ“‹ Features

  • User registration and login with JWT authentication
  • Real-time messaging via WebSocket/STOMP
  • Automatic token refresh
  • Protected routes
  • Responsive design with Material-UI
  • Message history
  • Type-safe codebase with TypeScript

πŸ—οΈ Project Structure

src/
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”œβ”€β”€ AuthHandler.tsx      # Authentication provider
β”‚   β”‚   β”œβ”€β”€ Login.tsx           # Login form
β”‚   β”‚   └── Register.tsx        # Registration form
β”‚   β”œβ”€β”€ chat/
β”‚   β”‚   β”œβ”€β”€ ChatView.tsx        # Main chat interface
β”‚   β”‚   β”œβ”€β”€ ChatMessage.tsx     # Individual message component
β”‚   β”‚   └── MessageContainer.tsx # Message list container
β”‚   β”œβ”€β”€ Layout.tsx              # Main layout wrapper
β”‚   β”œβ”€β”€ ProtectedRoute.tsx      # Route protection
β”‚   β”œβ”€β”€ Sidebar.tsx             # Navigation sidebar
β”‚   └── WebSocketHandler.tsx    # WebSocket connection management
β”œβ”€β”€ services/
β”‚   └── Api.tsx                 # Axios configuration
β”œβ”€β”€ store/
β”‚   β”œβ”€β”€ store.ts               # Redux store configuration
β”‚   β”œβ”€β”€ hooks.ts               # Typed Redux hooks
β”‚   └── slices/
β”‚       β”œβ”€β”€ authSlice.ts       # Authentication state
β”‚       β”œβ”€β”€ chatViewSlice.ts   # Chat messages state
β”‚       └── wsSlice.ts         # WebSocket state
β”œβ”€β”€ style/
β”‚   β”œβ”€β”€ index.css             # Global styles
β”‚   └── ChatMessage.css       # Message styling
β”œβ”€β”€ types/
β”‚   └── custom.d.ts           # Custom type definitions
β”œβ”€β”€ vite-env.d.ts             # Vite environment types
└── App.tsx                   # Main app component

πŸ› οΈ Installation & Setup

Prerequisites

  • Node.js (v22 or higher)
  • npm or yarn

Development Setup

  1. Clone the repository structure

    # Create parent directory
    mkdir chat-application
    cd chat-application
    
    # Clone both repositories
    git clone <frontend-repository-url> chatapp-react-frontend
    git clone <backend-repository-url> chatapp-spring-backend
  2. Install dependencies

    cd chatapp-react-frontend
    npm install
  3. Environment Configuration

    # Copy example environment file
    cp .env.example .env

    Configure the following variables in .env:

    VITE_API_BASE_URL=http://localhost:8080/api/v1
    VITE_WS_BASE_URL=http://localhost:8080/ws
  4. Start development server

    npm run dev
  5. Access the application

    • Open http://localhost:5173 in your browser

πŸ‹ Docker Setup

Development with Docker

# From parent directory containing both repos
cd chatapp-spring-backend
docker-compose -f docker-compose.dev.yml up --build

Production with Docker

# From parent directory containing both repos
cd chatapp-spring-backend
docker-compose up --build

πŸ”§ Configuration

API Configuration

The API base URL is configured in src/services/Api.tsx:

const api = axios.create({
  baseURL: env.VITE_API_BASE_URL,
  withCredentials: true
});

Redux Store

State management is configured in src/store/store.ts with three main slices:

  • authSlice - User authentication and token management
  • wsSlice - WebSocket connection state
  • chatViewSlice - Chat messages and UI state

Typed hooks are provided in src/store/hooks.ts for type-safe Redux usage:

export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

πŸ” Authentication Flow

  1. User registers/logs in via Login.tsx or Register.tsx
  2. JWT tokens are received and stored via AuthHandler
  3. Protected routes are accessed through ProtectedRoute
  4. Automatic token refresh handles expired access tokens

πŸ’¬ Real-time Messaging

The application uses WebSocket/STOMP for real-time communication:

  • Connection managed by WebSocketHandler
  • Messages sent to /app/chat endpoint
  • Real-time updates received from /topic/messages
  • Message state managed in chatViewSlice

πŸ“Š State Management

Auth State

{
   token: string | null,
   user: {
      email: string | null, 
      name: string | null,
      uid: string | null
   },
   isValidating: boolean,
}

Chat State

{
  chatViewCollection: [{
    viewId: number,
    title: string,
    messages: Message[],
    isLoading: boolean,
    error: string | null
  }]
}

WebSocket State

{
  stompClient: Client | null,
  connectionStatus: 'connected' | 'disconnected' | 'connecting' | 'error'
}

🎨 Styling

  • Material-UI (MUI) for modern, responsive components
  • Custom CSS in src/style/ for chat-specific styling
  • FontAwesome icons for UI elements
  • Dark theme with customizable Material-UI theming

πŸ“± Responsive Design

The application is fully responsive with:

  • Material-UI Grid system
  • Flexible chat container sizing
  • Responsive message bubbles
  • Adaptive sidebar layout

πŸ” Key Components

ChatView

The main chat interface that handles:

  • Message display and input
  • WebSocket subscription management
  • Real-time message updates
  • Send message functionality

MessageContainer

Displays chat messages with:

  • Auto-scrolling to latest messages
  • User/other message differentiation
  • Sender name display

AuthHandler

Manages authentication state:

  • Token storage and validation
  • Automatic token refresh
  • User session management
  • Request/response interceptors

πŸ“‹ Dependencies

Production Dependencies

  • react & react-dom - Core React (v19)
  • @reduxjs/toolkit & react-redux - State management
  • react-router-dom - Routing
  • axios - HTTP requests
  • @stomp/stompjs - WebSocket messaging
  • sockjs-client - SockJS WebSocket fallback
  • @mui/material - Material-UI components
  • @fortawesome/* - Icons

Development Dependencies

  • typescript - TypeScript compiler
  • vite - Build tool
  • eslint - Code linting
  • @typescript-eslint/* - TypeScript ESLint plugins
  • Various type definitions (@types/*)

πŸš€ Deployment

Using Docker (Recommended)

The application is containerized and deployed using Docker Compose from the backend repository.

Manual Deployment

  1. Build the application

    npm run build
  2. Deploy the dist folder to your web server (served by Nginx in production)

  3. Configure environment variables for production API URLs

πŸ› Troubleshooting

Common Issues

  1. WebSocket connection fails

    • Check backend WebSocket configuration
    • Verify CORS settings
    • Ensure backend is running
    • Check token is properly encoded in WebSocket URL
  2. Authentication issues

    • Check token expiration
    • Verify API endpoints
    • Clear browser storage if needed
    • Check localStorage for accessToken
  3. Build errors

    • Update dependencies
    • Check Node.js version compatibility

πŸ“š Documentation

Token Refresh Flowchart

Token Refresh Sequence Token refresh mechanism

Additional documentation available in the backend repository:

  • Architecture diagrams
  • Sequence diagrams
  • API integration guides

πŸ”— Related Projects

πŸ“„ License

This project is licensed under the MIT License.


Note: This frontend is designed to work with the corresponding Spring Boot backend. Ensure both applications are running for full functionality.