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.
- ๐ 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
- React.js
- Tailwind CSS
- Axios
- Socket.io Client
- Node.js
- Express.js
- Socket.io
- MongoDB
- Mongoose
- JWT Authentication
Chatify/
โ
โโโ client/
โ โโโ src/
โ โโโ components/
โ โโโ pages/
โ โโโ context/
โ โโโ hooks/
โ
โโโ server/
โ โโโ controllers/
โ โโโ routes/
โ โโโ middleware/
โ โโโ models/
โ โโโ socket/
โ โโโ config/
โ
โโโ package.json
โโโ README.md
git clone https://github.com/yourusername/chatify.gitcd chatifycd client
npm installcd ../server
npm installCreate a .env file inside the server folder.
PORT=5000
MONGO_URI=your_mongodb_connection
JWT_SECRET=your_secret_key
CLIENT_URL=http://localhost:3000npm run devnpm start User A
โ
โ
Socket.io Client
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
Persistent WebSocket
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
Express + Node.js
โ
Socket.io Server
โ
MongoDB Database
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
Persistent WebSocket
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
Socket.io Client
โ
User B
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 | 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 |
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.
- Real-time communication
- Low latency
- Automatic reconnection
- Event-based architecture
- Fallback support when WebSockets are unavailable
- Efficient bidirectional communication
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.
User Opens App
โ
โผ
Socket Connection Created
โ
โผ
Socket ID Assigned
โ
โผ
User Added to Active Connections
User Closes Browser
โ
โผ
disconnect Event Triggered
โ
โผ
Remove Socket Mapping
โ
โผ
Update Online Users
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"
}JWT is used for secure authentication.
Flow:
Login
โ
โผ
Generate JWT
โ
โผ
Store Token
โ
โผ
Protected API Requests
โ
โผ
Verify Token
โ
โผ
Access Granted
- JWT Authentication
- Password Hashing
- HTTPS Support
- Input Validation
- Message Sanitization
- Secure API Routes
- Protected Socket Connections
- Rate Limiting
- Environment Variables for Secrets
- CORS Configuration
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
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
- Multiple Node.js instances
- Redis Adapter for Socket.io
- Load Balancer
- Sticky Sessions
- Horizontal Scaling
- Database Indexing
- Message Queues (optional)
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
- Event-driven architecture
- Non-blocking I/O
- Efficient socket management
- Optimized MongoDB queries
- Connection cleanup
- Reduced unnecessary broadcasts
- Modular backend design
- ๐ฅ 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)
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
Contributions are welcome.
- Fork the repository
- Create a feature branch
git checkout -b feature-name- Commit your changes
git commit -m "Add feature"- Push the branch
git push origin feature-name- Open a Pull Request
This project is licensed under the MIT License.
Kaif Ali Khan
- GitHub: https://github.com/yourusername
- LinkedIn: https://linkedin.com/in/yourprofile