A production-ready realtime messaging application built using Node.js, Express.js, MongoDB, Socket.IO, React.js, and JWT Authentication.
- User Registration
- User Login
- JWT Access Token Authentication
- Refresh Token Support
- Protected API Routes
- Socket Authentication
- Email OTP Verification
- One-to-One Chat
- Instant Message Delivery
- Realtime Message Updates
- Online/Offline Presence
- Typing Indicators
- Seen Status
- Delivered Status
- Multi-Tab Support
- Automatic Reconnection
- Persistent Message Storage
- Encrypted Messages in Database
- Pagination Support
- Infinite Scroll Ready
- Fast Conversation Loading
- Optimized Query Structure
- Image Uploads
- Document Uploads
- File Preview Support
- Download Support
- Secure Upload Validation
- File Size Restrictions
- Password Hashing (bcrypt)
- JWT Authentication
- Refresh Token Validation
- Socket Authentication Middleware
- Protected Routes
- Input Validation
- File Validation
- Encrypted Message Storage
- Node.js
- Express.js
- MongoDB
- Mongoose
- Socket.IO
- JWT
- bcryptjs
- Multer
- React.js
- Vite
- Tailwind CSS
- Socket.IO Client
- Zustand
- Axios
- Framer Motion
backend/
│
├── src/
│ ├── controllers/
│ │ ├── auth.controller.js
│ │ ├── conversation.controller.js
│ │ ├── message.controller.js
│ │ └── upload.controller.js
│ │
│ ├── middleware/
│ │ ├── auth.middleware.js
│ │ └── upload.middleware.js
│ │
│ ├── models/
│ │ ├── user.model.js
│ │ ├── conversation.model.js
│ │ ├── message.model.js
│ │ └── otp.model.js
│ │
│ ├── repositories/
│ │ ├── conversation.repository.js
│ │ └── message.repository.js
│ │
│ ├── routes/
│ │ ├── auth.routes.js
│ │ ├── conversation.routes.js
│ │ ├── message.routes.js
│ │ ├── upload.routes.js
│ │ └── user.routes.js
│ │
│ ├── services/
│ │ └── otp.service.js
│ │
│ ├── socket/
│ │ ├── middleware/
│ │ │ └── socketAuth.js
│ │ │
│ │ ├── managers/
│ │ │ └── onlineUsers.manager.js
│ │ │
│ │ └── socket.js
│ │
│ ├── utils/
│ │ ├── encryption.js
│ │ └── token.js
│ │
│ └── app.js
│
└── uploads/
{
name,
email,
password,
avatar,
isVerified,
refreshToken
}{
participants,
lastMessage,
lastMessageSender,
lastMessageAt,
unreadCounts
}{
conversationId,
senderId,
receiverId,
message,
messageType,
fileUrl,
fileName,
fileSize,
mimeType,
status
}POST /api/auth/registerRequest
{
"name": "John",
"email": "john@example.com",
"password": "123456"
}POST /api/auth/loginRequest
{
"email": "john@example.com",
"password": "123456"
}GET /api/auth/meHeaders
Authorization: Bearer <token>POST /api/auth/refresh-tokenGET /api/conversationsPOST /api/conversations/startRequest
{
"userId": "receiver_user_id"
}GET /api/messages/:conversationId?page=1&limit=20POST /api/uploadBody
form-data
file: <file>
join_conversation
send_message
typing_start
typing_stop
message_seen
mark_conversation_read
receive_message
message_sent
typing_start
typing_stop
message_seen
online_users
conversations_updated
Create a .env file in backend directory.
PORT=5000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_access_token_secret
JWT_REFRESH_SECRET=your_refresh_token_secret
FRONTEND_URL=http://localhost:8080cd backend
npm install
npm run devcd frontend
npm install
npm run devJWT provides stateless authentication and simplifies API authorization.
Socket.IO provides realtime bidirectional communication, automatic reconnection, and room-based messaging.
Messages grow rapidly over time. Keeping them separate improves scalability and query performance.
It enables fast sidebar rendering without querying the Messages collection repeatedly.
Repositories isolate database logic from business logic and improve maintainability.
Messages are encrypted before storage to improve data security.
Pagination prevents loading large chat histories at once and improves performance.
The current architecture supports future improvements such as:
- Redis Adapter for Socket.IO
- Horizontal Scaling
- Message Queue Integration
- CDN-Based File Storage
- AWS S3 File Uploads
- Distributed Caching
- Microservice Migration
- Group Chats
- Voice Messages
- Video Calling
- Push Notifications
- Read Receipts Analytics
- Message Reactions
- End-to-End Encryption
- Cloud File Storage
- Redis Caching
- Admin Dashboard
Jeet Ahirwar
Realtime Messaging System Backend Assessment Project