Skip to content

algonomad571/NexusChat

Repository files navigation

Scalable Chat Application (Cloud-Ready)

A production-ready, real-time chat application built with modern web technologies, demonstrating enterprise-grade architecture patterns for handling thousands of concurrent users.

Chat Application

πŸš€ Overview

This scalable chat application showcases how to build a robust, real-time messaging system capable of handling high-traffic scenarios while maintaining low latency and reliable message delivery. The architecture is designed with cloud deployment and horizontal scaling in mind.

Key Highlights

  • Real-time Communication: WebSocket-based messaging with Socket.IO
  • Scalable Architecture: Modular backend design ready for horizontal scaling
  • Message Reliability: Queue-based message processing for fault tolerance
  • Security: JWT authentication and basic message encryption
  • Modern UI: Responsive React interface with beautiful animations
  • Load Testing: Built-in tools for performance benchmarking

πŸ›  Tech Stack

Backend

  • Node.js with Express.js - Event-driven server architecture
  • Socket.IO - WebSocket communication for real-time messaging
  • SQLite - Database (easily replaceable with PostgreSQL/MongoDB for production)
  • Redis-compatible Queue - Message queuing system (in-memory for demo)
  • JWT - Secure authentication
  • bcryptjs - Password hashing
  • Crypto-js - Message encryption

Frontend

  • React 18 - Modern UI library with hooks
  • TypeScript - Type-safe development
  • Tailwind CSS - Utility-first styling
  • Lucide React - Beautiful icons
  • Socket.IO Client - Real-time communication
  • date-fns - Date formatting utilities

Development & Testing

  • Vite - Fast development server and build tool
  • ESLint - Code linting
  • Load Testing - Custom Node.js load testing script

πŸ”§ Getting Started

Prerequisites

  • Node.js 16+
  • npm or yarn

Installation

  1. Clone and install dependencies

    npm install
  2. Start the development servers

    npm run dev

    This starts both the backend server (port 3001) and frontend dev server (port 5173).

  3. Open your browser Navigate to http://localhost:5173 to access the chat application.

First Time Setup

  1. Create an account - Register with any username/email/password
  2. Join the General room - You'll be automatically added to the default room
  3. Start chatting - Send messages and see real-time updates

πŸ— Architecture Overview

System Components

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   React Client  │◄──►│   WebSocket      │◄──►│  Message Queue  β”‚
β”‚   (Frontend)    β”‚    β”‚   Server         β”‚    β”‚   (Redis-like)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
                                β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚   Database       β”‚
                       β”‚   (SQLite/PG)    β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Services

  1. SocketHandler - Manages WebSocket connections and real-time events
  2. ChatService - Handles message processing and room management
  3. AuthService - JWT-based authentication and user management
  4. MessageQueue - Ensures reliable message delivery and scaling
  5. DatabaseManager - Data persistence and retrieval

Message Flow

  1. User sends message via React UI
  2. Socket.IO client emits to server
  3. Server validates and stores message in database
  4. Message published to queue for reliability
  5. Queue broadcasts to all connected clients in room
  6. Real-time UI updates across all connected users

πŸ”₯ Features

Real-time Messaging

  • Instant message delivery via WebSockets
  • Typing indicators showing when users are composing messages
  • Online/offline status indicators
  • Message read receipts and delivery confirmation

User Experience

  • Responsive Design - Works seamlessly on desktop and mobile
  • Dark Theme - Easy on the eyes with modern aesthetics
  • Smooth Animations - Polished micro-interactions
  • Auto-scroll - Messages automatically scroll to latest
  • Message Timestamps - Smart time formatting (Today, Yesterday, etc.)

Technical Features

  • JWT Authentication - Secure, stateless user sessions
  • Message Encryption - Basic encryption for message security
  • Connection Recovery - Automatic reconnection on network issues
  • Rate Limiting - Built-in protection against spam
  • Error Handling - Graceful degradation and error recovery

πŸ“Š Performance & Testing

Load Testing

Run the built-in load test to simulate concurrent users:

# Test with 50 users for 60 seconds, 0.5 messages/user/second
npm run test:load

# Custom test parameters: users, duration(s), message-rate
node tests/loadTest.js 100 120 1.0

Performance Metrics

The load tester tracks:

  • Concurrent Users - Number of simultaneous connections
  • Messages/Second - Real-time message throughput
  • Delivery Rate - Percentage of messages successfully delivered
  • Connection Success - User connection success rate
  • Error Rate - Failed operations and network errors

Expected Performance

  • 100 concurrent users - ~50 messages/second sustained
  • Message latency - <200ms average delivery time
  • Memory usage - ~100MB for 100 users (optimized for scaling)
  • CPU usage - <20% on modern hardware

πŸš€ Deployment & Scaling

Docker Deployment

# Backend Dockerfile example
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY backend/ .
EXPOSE 3001
CMD ["node", "server.js"]

Kubernetes Scaling

# Horizontal scaling example
apiVersion: apps/v1
kind: Deployment
metadata:
  name: chat-backend
spec:
  replicas: 3  # Scale to 3 instances
  selector:
    matchLabels:
      app: chat-backend
  template:
    spec:
      containers:
      - name: chat-backend
        image: chat-app:latest
        ports:
        - containerPort: 3001

Production Considerations

  1. Database: Replace SQLite with PostgreSQL/MongoDB with connection pooling
  2. Message Queue: Use Redis Cluster or Apache Kafka for high availability
  3. Load Balancing: Deploy behind nginx or AWS ALB with sticky sessions
  4. Caching: Add Redis for session storage and message caching
  5. Monitoring: Integrate Prometheus/Grafana for metrics and alerting
  6. Security: Add rate limiting, DDoS protection, and enhanced encryption

πŸ”’ Security Features

  • JWT Authentication - Secure, stateless authentication tokens
  • Password Hashing - bcrypt with salt for secure password storage
  • Message Encryption - AES encryption for message content
  • Input Validation - Server-side validation for all user inputs
  • CORS Configuration - Proper cross-origin resource sharing setup
  • SQL Injection Prevention - Parameterized queries for database safety

🎯 Use Cases

This application demonstrates solutions for:

  • Customer Support Chat - Real-time customer service systems
  • Team Collaboration - Internal communication platforms
  • Gaming Chat - In-game messaging systems
  • Social Platforms - Community chat features
  • Live Events - Real-time audience interaction
  • Educational Platforms - Classroom discussion tools

πŸ“ˆ Future Enhancements

Planned Features

  • File and image sharing with drag-drop upload
  • Voice and video calling integration
  • Message reactions and emoji responses
  • Advanced user roles and permissions
  • Message search and filtering
  • Push notifications for mobile apps
  • End-to-end encryption with key exchange
  • Message threading and replies
  • Custom chat themes and personalization
  • Analytics dashboard for chat metrics

Scaling Improvements

  • Redis Cluster integration for distributed caching
  • Apache Kafka for enterprise message queuing
  • MongoDB sharding for horizontal data scaling
  • CDN integration for file sharing
  • Microservices architecture with API Gateway
  • Auto-scaling based on concurrent user metrics

🀝 Contributing

Contributions are welcome! This project demonstrates enterprise patterns and is perfect for:

  • System Design Practice - Learn distributed systems concepts
  • Portfolio Projects - Showcase full-stack development skills
  • Interview Preparation - Demonstrate scalable architecture knowledge
  • Open Source Contributions - Add features and improvements

Development Setup

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes and add tests
  4. Commit changes: git commit -m 'Add amazing feature'
  5. Push to branch: git push origin feature/amazing-feature
  6. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Socket.IO - For excellent WebSocket library and documentation
  • React Team - For the powerful UI framework
  • Tailwind CSS - For the utility-first CSS framework
  • Node.js Community - For the rich ecosystem of packages
  • Open Source Contributors - For the amazing tools and libraries

πŸ“ž Support

If you encounter any issues or have questions:

  1. Check the Issues page for existing solutions
  2. Create a new issue with detailed description
  3. Include steps to reproduce and system information
  4. Tag with appropriate labels (bug, enhancement, question)

Happy Coding! πŸš€

About

Scalable Chat Application (Cloud-Ready)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published