Skip to content

Latest commit

ย 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ’ฌ Chatify - Real-Time Chat Application

A full-stack real-time chat application built using the MERN Stack and Socket.io. Chatify enables users to exchange messages instantly through persistent WebSocket connections, providing a seamless and responsive chatting experience.


๐Ÿš€ Features

  • ๐Ÿ” User Authentication
  • ๐Ÿ’ฌ Real-time one-to-one messaging
  • โšก Instant message delivery using WebSockets
  • ๐ŸŸข Online/Offline user status
  • ๐Ÿ“ก Persistent socket connections
  • ๐Ÿ“ฑ Responsive UI
  • ๐Ÿ›ก๏ธ Input validation and error handling
  • ๐Ÿ”„ Automatic socket reconnection
  • ๐Ÿ“ฆ Scalable backend architecture

๐Ÿ› ๏ธ Tech Stack

Frontend

  • React.js
  • Tailwind CSS
  • Axios
  • Socket.io Client

Backend

  • Node.js
  • Express.js
  • Socket.io
  • MongoDB
  • Mongoose
  • JWT Authentication

๐Ÿ“‚ Project Structure

Chatify/
โ”‚
โ”œโ”€โ”€ client/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”œโ”€โ”€ pages/
โ”‚   โ”œโ”€โ”€ context/
โ”‚   โ””โ”€โ”€ hooks/
โ”‚
โ”œโ”€โ”€ server/
โ”‚   โ”œโ”€โ”€ controllers/
โ”‚   โ”œโ”€โ”€ routes/
โ”‚   โ”œโ”€โ”€ middleware/
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ socket/
โ”‚   โ””โ”€โ”€ config/
โ”‚
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ README.md

โš™๏ธ Installation

Clone Repository

git clone https://github.com/yourusername/chatify.git
cd chatify

Install Frontend

cd client
npm install

Install Backend

cd ../server
npm install

Configure Environment Variables

Create a .env file inside the server folder.

PORT=5000

MONGO_URI=your_mongodb_connection

JWT_SECRET=your_secret_key

CLIENT_URL=http://localhost:3000

Run Backend

npm run dev

Run Frontend

npm start

๐Ÿ”„ Working Architecture

          User A
             โ”‚
             โ”‚
      Socket.io Client
             โ”‚
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
 Persistent WebSocket
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
             โ”‚
      Express + Node.js
             โ”‚
       Socket.io Server
             โ”‚
       MongoDB Database
             โ”‚
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
 Persistent WebSocket
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
             โ”‚
      Socket.io Client
             โ”‚
          User B

๐Ÿ“ฉ Message Flow

User sends message
        โ”‚
        โ–ผ
Socket emits event
        โ”‚
        โ–ผ
Server receives event
        โ”‚
        โ–ผ
Validate request
        โ”‚
        โ–ผ
Store message in MongoDB
        โ”‚
        โ–ผ
Find recipient socket
        โ”‚
        โ–ผ
Emit message event
        โ”‚
        โ–ผ
Recipient receives instantly

๐ŸŒ HTTP vs WebSocket

HTTP WebSocket
Request-Response Persistent Connection
Client always initiates communication Client and Server can communicate anytime
New connection for every request Single long-lived connection
Higher latency Very low latency
Suitable for CRUD APIs Suitable for Real-time Applications

โšก Why Socket.io?

Traditional REST APIs require clients to repeatedly request new messages (polling), which increases network traffic and latency.

Socket.io establishes a persistent WebSocket connection between the client and server, allowing the server to instantly push new messages to connected users without repeated HTTP requests.

Benefits

  • Real-time communication
  • Low latency
  • Automatic reconnection
  • Event-based architecture
  • Fallback support when WebSockets are unavailable
  • Efficient bidirectional communication

๐Ÿ‘ฅ Handling Multiple Users

When users connect:

  • Each client receives a unique Socket ID.
  • The server maps User IDs to Socket IDs.
  • Incoming messages are emitted only to the intended recipient.
  • Broadcasting to all connected users is avoided.

Example Mapping

User A โ†’ Socket ID 98ABCD

User B โ†’ Socket ID XY1234

User C โ†’ Socket ID PQ5678

When User A sends a message to User C, the server emits the event only to Socket ID PQ5678.


๐Ÿ”Œ Connection Lifecycle

Connection

User Opens App
      โ”‚
      โ–ผ
Socket Connection Created
      โ”‚
      โ–ผ
Socket ID Assigned
      โ”‚
      โ–ผ
User Added to Active Connections

Disconnection

User Closes Browser
      โ”‚
      โ–ผ
disconnect Event Triggered
      โ”‚
      โ–ผ
Remove Socket Mapping
      โ”‚
      โ–ผ
Update Online Users

๐Ÿ—„๏ธ Database

MongoDB stores:

  • User information
  • Chat messages
  • Conversation history
  • Authentication details

Example Message Document

{
  "senderId": "64ab...",
  "receiverId": "89cd...",
  "message": "Hello!",
  "timestamp": "2026-08-02T10:15:30Z"
}

๐Ÿ” Authentication

JWT is used for secure authentication.

Flow:

Login
   โ”‚
   โ–ผ
Generate JWT
   โ”‚
   โ–ผ
Store Token
   โ”‚
   โ–ผ
Protected API Requests
   โ”‚
   โ–ผ
Verify Token
   โ”‚
   โ–ผ
Access Granted

๐Ÿ›ก๏ธ Security Features

  • JWT Authentication
  • Password Hashing
  • HTTPS Support
  • Input Validation
  • Message Sanitization
  • Secure API Routes
  • Protected Socket Connections
  • Rate Limiting
  • Environment Variables for Secrets
  • CORS Configuration

โš™๏ธ Backend Improvements

Implemented several backend optimizations including:

  • Centralized error handling
  • Cleaner socket event management
  • Input validation
  • Efficient connection cleanup
  • Improved API structure
  • Reliable event broadcasting
  • Better modular architecture

๐Ÿ“ˆ Scalability

A single Node.js server can handle many concurrent connections, but at very large scale additional infrastructure is required.

Recommended architecture:

                 Users
                   โ”‚
                   โ–ผ
            Load Balancer
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ                โ–ผ
     Node Server 1     Node Server 2
          โ”‚                โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                 โ–ผ
         Redis Adapter
                 โ”‚
                 โ–ผ
             MongoDB

Scaling Techniques

  • Multiple Node.js instances
  • Redis Adapter for Socket.io
  • Load Balancer
  • Sticky Sessions
  • Horizontal Scaling
  • Database Indexing
  • Message Queues (optional)

โšก Why Node.js?

Node.js follows an event-driven, non-blocking I/O architecture, making it highly suitable for applications requiring numerous simultaneous connections.

Advantages:

  • Handles thousands of concurrent connections
  • Fast asynchronous execution
  • Efficient event loop
  • Lightweight threads
  • Excellent for real-time communication

๐Ÿ“Š Performance Optimizations

  • Event-driven architecture
  • Non-blocking I/O
  • Efficient socket management
  • Optimized MongoDB queries
  • Connection cleanup
  • Reduced unnecessary broadcasts
  • Modular backend design

๐Ÿ”ฎ Future Enhancements

  • ๐Ÿ‘ฅ Group Chats
  • ๐Ÿ“ž Voice Calling
  • ๐ŸŽฅ Video Calling
  • ๐Ÿ“Ž File Sharing
  • ๐Ÿ˜Š Emoji Reactions
  • ๐Ÿ“ Typing Indicators
  • โœ”๏ธ Read Receipts
  • ๐Ÿ” Message Search
  • ๐Ÿ“‚ Media Gallery
  • ๐Ÿ”” Push Notifications
  • ๐ŸŒ™ Dark Mode
  • โœ๏ธ Edit/Delete Messages
  • ๐Ÿ“ฑ Progressive Web App (PWA)

๐Ÿ“š Interview Questions Covered

This project demonstrates understanding of:

  • MERN Stack
  • Express.js
  • MongoDB
  • Node.js
  • Socket.io
  • WebSockets
  • REST APIs
  • Event-Driven Architecture
  • JWT Authentication
  • Backend Scalability
  • Redis Adapter
  • Load Balancing
  • Sticky Sessions
  • Error Handling
  • Input Validation
  • Real-time Systems Design

๐Ÿค Contributing

Contributions are welcome.

  1. Fork the repository
  2. Create a feature branch
git checkout -b feature-name
  1. Commit your changes
git commit -m "Add feature"
  1. Push the branch
git push origin feature-name
  1. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License.


๐Ÿ‘จโ€๐Ÿ’ป Author

Kaif Ali Khan


โญ If you found this project helpful, consider giving it a star!

About

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages