Skip to content

Repository files navigation

Real-time Chat Application with Streaming

A full-featured real-time chat application built with Node.js, Socket.IO, MongoDB, and Docker. Features include real-time messaging, file sharing with streaming downloads, user presence indicators, and typing notifications.

πŸš€ Features

  • Real-time Messaging: Instant message delivery using Socket.IO
  • File Sharing: Upload and download files with streaming support
  • User Presence: See who's online and when they last seen
  • Typing Indicators: Real-time typing notifications
  • Message History: Persistent message storage in MongoDB
  • Responsive Design: Works on desktop and mobile devices
  • Docker Support: Easy deployment with Docker and Docker Compose
  • Production Ready: Nginx reverse proxy and SSL support

πŸ› οΈ Tech Stack

  • Backend: Node.js, Express.js
  • Real-time Communication: Socket.IO
  • Database: MongoDB with Mongoose ODM
  • Frontend: Vanilla JavaScript, HTML5, CSS3
  • File Handling: Multer for uploads, streaming for downloads
  • Containerization: Docker & Docker Compose
  • Reverse Proxy: Nginx
  • Deployment: DigitalOcean ready

πŸ“¦ Installation

Prerequisites

  • Node.js (v16 or higher)
  • MongoDB (v4.4 or higher)
  • Docker & Docker Compose (optional)

Local Development

  1. Clone the repository

    git clone <repository-url>
    cd realtime-chat-streaming
  2. Install dependencies

    npm install
  3. Set up environment variables

    cp .env.example .env

    Edit .env file with your configuration.

  4. Start MongoDB Make sure MongoDB is running on your system.

  5. Run the application

    # Development mode with auto-restart
    npm run dev
    
    # Production mode
    npm start
  6. Access the application Open your browser and go to http://localhost:3000

Docker Deployment

  1. Build and run with Docker Compose

    docker-compose up -d
  2. Access the application Open your browser and go to http://localhost

🌐 DigitalOcean Deployment

1. Create a DigitalOcean Droplet

  1. Create a new Ubuntu 20.04 droplet
  2. Choose appropriate size (minimum 1GB RAM recommended)
  3. Add your SSH key

2. Prepare the Server

# Update the system
sudo apt update && sudo apt upgrade -y

# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER

# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/v2.21.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# Reboot to apply Docker group changes
sudo reboot

3. Deploy the Application

# Clone your repository
git clone <your-repository-url>
cd realtime-chat-streaming

# Set up environment variables for production
cp .env.example .env
nano .env  # Edit with production values

# Start the application
docker-compose up -d

4. Set up Domain and SSL (Optional)

  1. Point your domain to the droplet's IP address
  2. Install Certbot for Let's Encrypt SSL:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com

πŸ“ Project Structure

realtime-chat-streaming/
β”œβ”€β”€ public/
β”‚   └── index.html          # Frontend application
β”œβ”€β”€ uploads/                # File upload directory
β”œβ”€β”€ .env.example           # Environment variables template
β”œβ”€β”€ .gitignore            # Git ignore rules
β”œβ”€β”€ docker-compose.yml    # Docker Compose configuration
β”œβ”€β”€ Dockerfile           # Docker image configuration
β”œβ”€β”€ nginx.conf          # Nginx reverse proxy config
β”œβ”€β”€ package.json        # Node.js dependencies
β”œβ”€β”€ server.js          # Main application server
└── README.md         # This file

πŸ”§ Configuration

Environment Variables

Variable Description Default
PORT Server port 3000
NODE_ENV Environment development
MONGODB_URI MongoDB connection string mongodb://localhost:27017/chat-app
JWT_SECRET JWT signing secret Required for auth
MAX_FILE_SIZE Maximum file upload size 10485760 (10MB)

File Upload Limits

  • Maximum file size: 10MB (configurable)
  • Supported file types: All types
  • Storage: Local filesystem (can be extended to cloud storage)

πŸš€ Features in Detail

Real-time Messaging

  • Instant message delivery using WebSocket connections
  • Message persistence in MongoDB
  • Message history loading for new users

File Sharing

  • Drag & drop file uploads
  • Progress indicators during upload
  • Streaming downloads with range request support
  • File type detection and icons

User Experience

  • Responsive design for all screen sizes
  • Typing indicators
  • User presence (online/offline status)
  • Message timestamps
  • Auto-scrolling message container

Performance & Security

  • Rate limiting on API endpoints
  • File size restrictions
  • Input sanitization
  • CORS configuration
  • Nginx reverse proxy for production

πŸ”’ Security Considerations

  • Input validation and sanitization
  • File upload restrictions
  • Rate limiting
  • CORS configuration
  • Environment variable protection
  • Non-root Docker user

πŸ§ͺ Testing

Manual Testing

  1. Open multiple browser tabs/windows
  2. Enter different usernames
  3. Test messaging between users
  4. Test file upload and download
  5. Test typing indicators
  6. Test user presence indicators

Load Testing

Use tools like Artillery or WebSocket King to test concurrent connections:

# Install Artillery
npm install -g artillery

# Run load test (create artillery-config.yml first)
artillery run artillery-config.yml

πŸ“ˆ Monitoring

Application Monitoring

  • MongoDB connection status
  • WebSocket connection count
  • File upload/download metrics
  • Error logging

Infrastructure Monitoring (DigitalOcean)

  • Droplet CPU and memory usage
  • Disk space monitoring
  • Network traffic
  • Docker container health

πŸ”„ Updates and Maintenance

Updating the Application

# Pull latest changes
git pull origin main

# Rebuild and restart containers
docker-compose down
docker-compose up -d --build

Database Backups

# Create MongoDB backup
docker exec -it <mongo-container-id> mongodump --db chat-app --out /backup

# Restore MongoDB backup
docker exec -it <mongo-container-id> mongorestore --db chat-app /backup/chat-app

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

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

πŸ†˜ Troubleshooting

Common Issues

  1. MongoDB Connection Error

    • Check if MongoDB is running
    • Verify MONGODB_URI in .env file
    • Check network connectivity
  2. File Upload Issues

    • Check uploads directory permissions
    • Verify MAX_FILE_SIZE setting
    • Check disk space
  3. WebSocket Connection Issues

    • Check firewall settings
    • Verify port 3000 is accessible
    • Check CORS configuration
  4. Docker Issues

    • Ensure Docker is running
    • Check Docker Compose version
    • Verify port conflicts

Logs

# View application logs
docker-compose logs app

# View MongoDB logs
docker-compose logs mongo

# View Nginx logs
docker-compose logs nginx

# Follow logs in real-time
docker-compose logs -f

πŸ“§ Support

If you encounter any issues or have questions, please:

  1. Check the troubleshooting section
  2. Search existing issues
  3. Create a new issue with detailed information
  4. Join our community discussions

Happy Chatting! πŸŽ‰

About

Real-time chat application with secure authentication, file streaming, Socket.IO, MongoDB, and Docker support

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages