Skip to content

Latest commit

Β 

History

History
338 lines (262 loc) Β· 8.81 KB

File metadata and controls

338 lines (262 loc) Β· 8.81 KB

Real-Time Chat Application

A modern, real-time chat application built with ASP.NET Core, SignalR, React, and PostgreSQL. Features include real-time messaging, online status tracking, JWT authentication with automatic token refresh, and a beautiful dark mode UI.

✨ Features

  • πŸ” JWT Authentication with automatic token refresh
  • πŸ’¬ Real-Time Messaging using SignalR
  • 🟒 Online/Offline Status tracking
  • πŸ‘₯ User Discovery with live registration notifications
  • 🎨 Modern Dark Mode UI with gradient accents
  • πŸ“± Fully Responsive - works on desktop, tablet, and mobile
  • πŸ”„ Auto-Reconnection with exponential backoff
  • 🎯 Optimistic UI Updates for better UX

πŸ“Έ Screenshots

Chat Interface

Chat Interface

User List & Online Status

User List

Dark Mode Theme

Dark Mode

Real-Time Messaging

Real-Time Messages

Responsive Design

Mobile View

πŸ› οΈ Tech Stack

Backend

  • ASP.NET Core 9.0 - Web API framework
  • SignalR - Real-time communication
  • Entity Framework Core - ORM
  • PostgreSQL - Database
  • JWT Bearer Authentication - Security

Frontend

  • React 18+ - UI library
  • Vite - Build tool and dev server
  • Axios - HTTP client with interceptors
  • @microsoft/signalr - SignalR client
  • React Router - Client-side routing

πŸ“‹ Prerequisites

πŸš€ Getting Started

1. Clone the Repository

git clone <your-repo-url>
cd ChatAsp

2. Backend Setup

Configure Environment

  1. Navigate to the backend folder:
cd MyApp
  1. Copy the example configuration:
cp appsettings.Example.json appsettings.json
  1. Edit appsettings.json with your settings:
{
  "ConnectionStrings": {
    "Default": "Host=localhost;Port=5432;Database=asp_chat_db;Username=your_username;Password=your_password"
  },
  "AppSettings": {
    "Token": "your-super-secret-jwt-key-minimum-32-characters-long",
    "Issuer": "YourAppIssuer",
    "Audience": "YourAppAudience"
  },
  "Cors": {
    "AllowedOrigins": "http://localhost:5173"
  }
}

Create Database

  1. Create a PostgreSQL database:
createdb asp_chat_db
  1. Run migrations:
dotnet ef database update

Or if you don't have EF tools installed:

dotnet tool install --global dotnet-ef
dotnet ef database update

Run Backend

dotnet run

Backend will start on http://localhost:5000

3. Frontend Setup

Configure Environment

  1. Navigate to the frontend folder:
cd frontend
  1. Copy the example environment file:
cp .env.example .env
  1. The .env file should contain:
VITE_API_BASE_URL=http://localhost:5000/api
VITE_SIGNALR_HUB_URL=http://localhost:5000/hub/chat

Install Dependencies

npm install

Run Frontend

npm run dev

Frontend will start on http://localhost:5173

πŸ“ Project Structure

ChatAsp/
β”œβ”€β”€ MyApp/                          # Backend (ASP.NET Core)
β”‚   β”œβ”€β”€ Controllers/
β”‚   β”‚   └── AuthController.cs       # Authentication endpoints
β”‚   β”œβ”€β”€ Hubs/
β”‚   β”‚   └── ChatHub.cs              # SignalR hub for real-time communication
β”‚   β”œβ”€β”€ Infrastructure/
β”‚   β”‚   └── PasswordHasher.cs       # Password hashing utilities
β”‚   β”œβ”€β”€ Migrations/                 # EF Core migrations
β”‚   β”œβ”€β”€ Models/
β”‚   β”‚   β”œβ”€β”€ AppDbContext.cs         # Database context
β”‚   β”‚   β”œβ”€β”€ AppUser.cs              # User entity
β”‚   β”‚   β”œβ”€β”€ AppRoom.cs              # Chat room entity
β”‚   β”‚   β”œβ”€β”€ AppMessage.cs           # Message entity
β”‚   β”‚   └── AppUserConnection.cs    # SignalR connection tracking
β”‚   β”œβ”€β”€ Services/
β”‚   β”‚   β”œβ”€β”€ AuthService.cs          # Authentication logic
β”‚   β”‚   β”œβ”€β”€ UserService.cs          # User management
β”‚   β”‚   β”œβ”€β”€ RealtimeService.cs      # SignalR data access
β”‚   β”‚   └── DTOS/                   # Data transfer objects
β”‚   β”œβ”€β”€ appsettings.Example.json    # Example configuration
β”‚   └── Program.cs                  # Application entry point
β”‚
└── frontend/                       # Frontend (React)
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ components/
    β”‚   β”‚   β”œβ”€β”€ auth/               # Authentication components
    β”‚   β”‚   └── home/               # Main app components
    β”‚   β”‚       β”œβ”€β”€ Home.jsx        # Main layout
    β”‚   β”‚       β”œβ”€β”€ ChatModal.jsx   # Chat interface
    β”‚   β”‚       β”œβ”€β”€ ConversationsSidebar.jsx  # Active chats
    β”‚   β”‚       └── UserList.jsx    # User discovery
    β”‚   β”œβ”€β”€ services/
    β”‚   β”‚   β”œβ”€β”€ AxiosProvider.js    # HTTP client with auth
    β”‚   β”‚   └── SignalRService.js   # SignalR client
    β”‚   └── App.jsx                 # Root component
    β”œβ”€β”€ .env.example                # Example environment variables
    └── package.json

πŸ” Security Features

JWT Authentication

  • Access tokens (short-lived, ~15min)
  • Refresh tokens (long-lived, ~7 days)
  • Automatic token refresh on 401 errors
  • Secure password hashing with BCrypt

SignalR Security

  • JWT authentication for WebSocket connections
  • User-scoped message delivery
  • Connection tracking per user

🎨 UI/UX Features

Dark Mode Theme

  • Modern slate/navy color scheme
  • Purple/pink gradient accents
  • High contrast for readability
  • Custom scrollbars

Responsive Design

  • Desktop (1200px+): Two-column layout with sidebar
  • Tablet (768px-1199px): Stacked layout, compact header
  • Mobile (< 768px): Single column, touch-optimized
  • Small Mobile (< 480px): Ultra-compact, icon-only buttons

Animations

  • Smooth transitions and micro-interactions
  • Slide-in modals and messages
  • Fade effects for status changes
  • Loading spinners

πŸ”§ Configuration

Backend Configuration (appsettings.json)

{
  "ConnectionStrings": {
    "Default": "PostgreSQL connection string"
  },
  "AppSettings": {
    "Token": "JWT secret key (min 32 chars)",
    "Issuer": "JWT issuer",
    "Audience": "JWT audience"
  },
  "Cors": {
    "AllowedOrigins": "Comma-separated list of allowed origins"
  }
}

Frontend Configuration (.env)

VITE_API_BASE_URL=http://localhost:5000/api
VITE_SIGNALR_HUB_URL=http://localhost:5000/hub/chat

πŸ“‘ API Endpoints

Authentication

  • POST /api/auth/register - Register new user
  • POST /api/auth/login - Login user
  • GET /api/auth/refresh-token?token=... - Refresh access token

Users

  • GET /api/user/users - List all users (authenticated)

Chat

  • GET /api/chat/messages/{roomId} - Get message history
  • GET /api/chat/rooms/{userId} - Get user's chat rooms

SignalR Hub (/hub/chat)

  • CreateOrJoinChatRoom(int otherUserId) - Start/join chat
  • SendMessage(int roomId, string messageContent) - Send message
  • Events: ReceiveMessage, UserJoined, UserStatusChanged, UserRegistered

🚒 Deployment

Backend (Production)

  1. Update appsettings.json with production database and secrets
  2. Set ASPNETCORE_ENVIRONMENT=Production
  3. Build the app:
dotnet publish -c Release
  1. Run with Kestrel or deploy to Azure/AWS/Docker

Frontend (Production)

  1. Update .env with production API URL:
VITE_API_BASE_URL=https://your-api.com/api
VITE_SIGNALR_HUB_URL=https://your-api.com/hub/chat
  1. Build:
npm run build
  1. Deploy the dist/ folder to Vercel, Netlify, or any static host

πŸ› Troubleshooting

Hot Reload Issues

If you encounter HotReloadException errors:

# Stop dotnet watch and restart
dotnet run

SignalR Connection Fails

  • Verify JWT token is valid
  • Check CORS configuration matches frontend URL
  • Ensure WebSocket support is enabled

Database Migration Errors

# Reset database (WARNING: deletes all data)
dotnet ef database drop
dotnet ef database update

πŸ“ License

This project is open source and available under the MIT License.

πŸ‘€ Author

Built as a learning project to explore SignalR, real-time web applications, and modern React patterns.

πŸ™ Acknowledgments

  • ASP.NET Core Team for SignalR
  • React Team for the amazing framework
  • Tailwind inspiration for the color scheme