A full-featured real-time chat application built using Next.js, Express.js, MongoDB, RabbitMQ, Redis, Socket.IO, and deployed on AWS. The project follows a microservices architecture and uses npm as the package manager.
- About
- Architecture Overview
- Tech Stack
- Getting Started
- Environment Variables
- RabbitMQ Setup
- Running Services (development)
- API Endpoints (example)
- Socket.IO Events (example)
- Redis Usage
- Testing
- Deployment
- Logging & Monitoring
- Contributing
- License
This project is a real-time messaging platform that enables one-on-one chats with support for text and image messages. It demonstrates a microservice design with event-driven communication via RabbitMQ and real-time delivery via Socket.IO. MongoDB stores persistent data, Redis is used for caching and ephemeral data (presence, sockets), and services are intended to be deployed on AWS for production.
Next.js (Frontend)
β
βΌ
ββββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ
β Auth Service β Chat Service β User Service β
ββββββββββββββββ΄βββββββββββββββ΄βββββββββββββββ
β
RabbitMQ (Event Bus)
β
Redis (Caching / PubSub)
β
MongoDB (Database)
- Frontend (Next.js): UI, authentication, client-side socket connection.
- Auth Service: user registration/login, JWT issuance/refresh.
- User Service: user profile, contact lists, search.
- Chat Service: message persistence, message retrieval, integrations with Socket.IO and RabbitMQ.
- RabbitMQ: event bus for notifications, message-delivery events, analytics events, and inter-service commands.
- Redis: session store, presence (which user is online), socketId mapping, and fast cache for recent messages.
- MongoDB: primary datastore for users, chats, messages, and metadata.
- Frontend: Next.js (React)
- Backend / Gateway: Express.js (Node.js)
- Services: Node.js + Express microservices
- Database: MongoDB (can use MongoDB Atlas)
- Message Broker: RabbitMQ
- Cache / PubSub: Redis (ElastiCache in AWS)
- Realtime: Socket.IO
- Storage (images): AWS S3 (or similar)
- CI / CD: GitHub Actions (suggested)
- Deployment: AWS (EC2, ECS, or EKS), S3, Load Balancer, Route53
- Package Manager: npm
Follow these steps to get a local development environment running.
Make sure you have installed:
# Node and npm
node --version # v18+ recommended
npm --version
# Docker (for RabbitMQ, Redis, and optionally MongoDB)
docker --version
# (Optional) MongoDB locally or use MongoDB Atlas
mongod --versionA recommended monorepo layout (adjust to your project):
/repo-root
/services
/auth-service
/chat-service
/user-service
/gateway # express gateway / api
/frontend # next.js app
/infrastructure # docker-compose, k8s manifests, terraform, etc
README.md
Clone the repo and install root dependencies where needed.
git clone https://github.com/yourusername/realtime-chat-app.git
cd realtime-chat-appInstall dependencies for each service and frontend. Example for each package.json folder:
# in frontend
cd frontend
npm install
# in gateway
cd ../gateway
npm install
# in services/auth-service
cd ../services/auth-service
npm install
# repeat for other services...Tip: use terminal tabs or a process manager (tmux) to run multiple services concurrently.
Create .env files for each service. Example variables (replace values as necessary).
PORT=5000
MONGO_URI=mongodb://localhost:27017/chatapp
JWT_SECRET=your_jwt_secret
RABBITMQ_URL=amqp://admin:admin123@localhost:5672
REDIS_URL=redis://localhost:6379
S3_BUCKET_NAME=your-s3-bucket
S3_REGION=ap-south-1
S3_ACCESS_KEY=your-access-key
S3_SECRET_KEY=your-secret-key
NEXT_PUBLIC_API_URL=http://localhost:5000
NEXT_PUBLIC_SOCKET_URL=http://localhost:5000
Start RabbitMQ with management UI (recommended for local dev):
docker run -d --hostname rabbitmq-host --name rabbitmq-container -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin123 -p 5672:5672 -p 15672:15672 rabbitmq:3-managementManagement UI: http://localhost:15672 (admin / admin123)
If you use docker-compose, add rabbitmq, redis, and mongo services to the compose file.
Example start commands (adjust to your package.json scripts):
# In frontend
cd frontend
npm run dev
# In gateway
cd ../gateway
npm run dev
# In chat service
cd ../services/chat-service
npm run dev
# In auth service
cd ../services/auth-service
npm run devYou can also create a root-level npm script that uses concurrently or pm2 to run multiple services.
These are example routes β adapt to your implementation.
POST /api/v1/auth/registerβ Register userPOST /api/v1/auth/loginβ Login (returns JWT)POST /api/v1/auth/refreshβ Refresh token
GET /api/v1/users/:idβ Get user profileGET /api/v1/users/search?q=...β Search users
GET /api/v1/chat/allβ Get chat listPOST /api/v1/chat/newβ Start new chatGET /api/v1/chat/message/:chatIdβ Fetch messagesPOST /api/v1/chat/messageβ Send a message (multipart/form-data)POST /api/v1/chat/seenβ Mark messages as seen
Client emits / server listens:
connectβ Socket connectjoinβ join chat room (payload:{ chatId, userId })typingβ typing indicatorsendMessageβ send message (payload:{ chatId, text, imageUrl? })
Server emits / client listens:
messageβ new message broadcastmessage:deliveredβ delivery confirmationmessage:seenβ seen updatesuser:online/user:offlineβ presence updatestypingβ typing updates
- Map
userId -> socketId(s)to deliver messages to connected sockets. - Cache recent messages for faster read (e.g., last 50 messages of a chat).
- Use Redis pub/sub for cross-node socket event propagation (if scaling Socket.IO across multiple instances).
- Unit tests: Jest (or preferred test runner)
- Integration tests: supertest for API endpoints
- End-to-end: Playwright / Cypress for frontend flows
Example to run tests (if configured):
npm testGuidelines for deploying to AWS:
- Use MongoDB Atlas (or DocumentDB) for managed MongoDB.
- Use ElastiCache (Redis) for Redis.
- Use Amazon MQ or managed RabbitMQ (or self-host on EC2/ECS).
- Deploy services as Docker containers on ECS Fargate or EKS.
- Use S3 + CloudFront for serving Next.js static assets (or use Next.js serverless options).
- Manage secrets with AWS Secrets Manager or SSM Parameter Store.
- Use auto-scaling groups / ECS service autoscaling for throughput.
Build & start (example):
# build frontend for production
cd frontend
npm run build
npm start
# backend services
cd ../gateway
npm run build
npm start- Use Winston / Pino for structured logging on backend services.
- Aggregate logs via CloudWatch Logs (AWS) or ELK stack.
- Use Prometheus + Grafana for metrics, or CloudWatch metrics.
- Setup health-check endpoints for each service and let the load balancer use them.
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Commit changes:
git commit -m "feat: add ..." - Push:
git push origin feature/your-feature - Open a PR and describe your changes
See CONTRIBUTING.md for more details.
This project is licensed under the MIT License. See the LICENSE file for details.