A simple chatroom web application with a NextJS frontend and .NET Core backend, using MariaDB as the database.
- Frontend: NextJS 14, SASS, npm
- Backend: .NET 8 (ASP.NET Core)
- Database: MariaDB 11.2
- Deployment: Debian 12 (Bookworm)
- Testing: Jest (frontend), xUnit (backend)
- Real-time chat functionality using SignalR
- Persistent message storage in MariaDB
- Responsive design using SASS
- User authentication (simple username-based)
- Typing indicators with real-time updates
- Join/leave notifications
- Error handling and loading states
- HTTPS support for production
- Pagination for message history
- Rate limiting to prevent abuse
- Health checks for monitoring
- API service layer with proper error handling
- SignalR service for real-time communication
- Content sanitization to prevent XSS attacks
- Error boundaries for graceful error handling
- Security headers for browser protection
- Input validation on both client and server sides
- Automated testing for both frontend and backend
- Reconnection handling for network interruptions
- Auto-scroll with smart behavior based on user interaction
- Active users list with online status
- Message editing and deletion capabilities
- Message reactions with emoji support
- Message search functionality
- Browser notifications for new messages
- Username uniqueness validation
- Read receipts and edit timestamps
./
├── frontend/ # NextJS frontend application
│ ├── app/ # Next.js app directory
│ │ ├── components/ # Reusable UI components
│ │ ├── chat/ # Chat page and functionality
│ │ ├── services/ # API and SignalR services
│ │ ├── styles/ # SASS stylesheets
│ ├── public/ # Static assets
│ ├── jest.setup.js # Jest testing setup
├── backend/ # .NET Core backend API
│ ├── Controllers/ # API controllers
│ ├── Data/ # Database context
│ ├── Hubs/ # SignalR hubs
│ ├── Middleware/ # Custom middleware
│ ├── Models/ # Domain models and DTOs
│ ├── Services/ # Business logic services
│ ├── ChatRoom.Api.Tests/ # Backend unit tests
├── docs/ # Documentation
└── scripts/ # Deployment and utility scripts
- Node.js 20.x or later
- .NET SDK 8.0 or later
- MariaDB 11.2 or later
# Navigate to the frontend directory
cd frontend
# Create .env.local file from example
cp .env.local.example .env.local
# Install dependencies
npm install
# Start the development server
npm run devThe frontend will be available at http://localhost:3000.
# Navigate to the backend directory
cd backend
# Restore dependencies
dotnet restore
# Start the development server
dotnet runThe backend API will be available at http://localhost:5000.
- Install MariaDB on your system
- Create a new database and user:
CREATE DATABASE chatroom_dev;
CREATE USER 'chatroomuser'@'localhost' IDENTIFIED BY 'dev_password';
GRANT ALL PRIVILEGES ON chatroom_dev.* TO 'chatroomuser'@'localhost';
FLUSH PRIVILEGES;- The connection string is already set in
appsettings.Development.json
# Navigate to the frontend directory
cd frontend
# Run tests once
npm test
# Run tests in watch mode during development
npm run test:watch# Navigate to the backend directory
cd backend
# Run tests
dotnet testBefore deployment, set the following environment variables:
export REPO_PATH=/path/to/your/repo
export DOMAIN=your-domain.com
export MARIADB_PASSWORD=your_secure_password
export DB_USER=chatroomuser # Optional, defaults to chatroomuser
export DB_NAME=chatroom # Optional, defaults to chatroom# Make the scripts executable
chmod +x scripts/deploy.sh
chmod +x scripts/setup_https.sh
chmod +x scripts/backup_db.sh
# Run the deployment script (as root or with sudo)
sudo ./scripts/deploy.sh
# Set up HTTPS (optional but recommended for production)
sudo ./scripts/setup_https.shTo set up automatic database backups, add the backup script to cron:
# Run backups daily at 2 AM
echo "0 2 * * * root MARIADB_PASSWORD=your_secure_password /path/to/repo/scripts/backup_db.sh" > /etc/cron.d/chatroom-backupsGET /api/messages- Get all messages with pagination- Query parameters:
page(default: 1),pageSize(default: 50, max: 100)
- Query parameters:
GET /api/messages/{id}- Get a specific message by IDPOST /api/messages- Create a new messageGET /health- Health check endpoint
The application uses SignalR for real-time communication. The SignalR hub is available at /chatHub with the following methods:
JoinRoom- Join the chat roomSendMessage- Send a messageSendTypingStatus- Send typing statusEditMessage- Edit an existing messageDeleteMessage- Delete a messageAddReaction- Add an emoji reaction to a messageRemoveReaction- Remove an emoji reaction from a messageGetReactions- Get all reactions for a messageGetActiveUsers- Get a list of active users in the chat
message- Receive a new messagetypingStatus- Receive typing status updatesmessageEdited- Message edited notificationmessageDeleted- Message deleted notificationmessageReactions- Updated reactions for a messageactiveUsers- Updated list of active users
- Environment-specific configuration
- HTTPS support with automatic certificate renewal
- Security headers for protection against common web vulnerabilities
- Proper error handling with detailed client feedback
- Input validation with regex patterns
- Rate limiting to prevent abuse
- Database connection string stored as environment variable
- Content sanitization to prevent XSS attacks
- CORS configuration with proper origins
- Automatic reconnection with exponential backoff
- Username uniqueness validation
- Proper input sanitization
- Added active users list with online status display
- Implemented message editing and deletion capabilities
- Added emoji reactions to messages
- Added message search functionality
- Implemented browser notifications for new messages
- Added typing indicator timeout to prevent ghost indicators
- Fixed username uniqueness validation
- Enhanced error handling in both frontend and backend
- Improved input sanitization to prevent XSS attacks
- Enhanced SignalR service with comprehensive event handling
- Improved reconnection logic with exponential backoff
- Added read receipts and edit timestamps
- Improved responsive design for mobile devices
Some potential future enhancements include:
- Advanced user authentication with user accounts and JWT tokens
- File sharing capabilities for images and documents
- Multiple chat rooms support
- User profiles with avatars and custom settings
- Message threading for replies to specific messages
- Rich text formatting support (Markdown)
- End-to-end encryption for private conversations
MIT