Skip to content

theUtkarshRaj/RelayChat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ’ฌ RelayChat

A real-time chat application built with a microservices architecture, featuring email-based OTP authentication, live messaging, image sharing, typing indicators, and online presence tracking.


๐Ÿงฉ Problem It Solves

Traditional chat apps bundle all logic into a single monolith, making them hard to scale and maintain. RelayChat separates concerns into independent services โ€” a User Service, a Chat Service, and a Mail Service โ€” each deployable and scalable independently.


โœจ Key Features

  • ๐Ÿ“ง Email OTP Login โ€” Passwordless authentication via 6-digit OTP sent to email
  • โšก Real-time Messaging โ€” Instant delivery via Socket.IO
  • ๐Ÿ–ผ๏ธ Image Sharing โ€” Upload and send images via Cloudinary
  • โœ… Read Receipts โ€” Messages are marked as seen with timestamps
  • ๐ŸŸข Online Presence โ€” See which users are currently online
  • โŒจ๏ธ Typing Indicators โ€” Live "user is typing..." feedback
  • ๐Ÿ” JWT Authentication โ€” Stateless auth with 15-day token expiry
  • ๐Ÿšฆ OTP Rate Limiting โ€” Redis-backed 1 OTP per email per minute
  • ๐Ÿ‘ค Profile Management โ€” Update your display name

๐Ÿ› ๏ธ Tech Stack

Layer Technology
Frontend Next.js 16 (App Router), React 19, TypeScript, Tailwind CSS
Backend Node.js, Express.js v5, TypeScript
Real-time Socket.IO
Database MongoDB (Mongoose)
Cache / Rate Limit Redis (Upstash)
Message Queue RabbitMQ (via AMQP / Docker)
Email Nodemailer (Gmail SMTP)
Media Upload Cloudinary + Multer
Auth JWT (jsonwebtoken)

๐Ÿ—๏ธ System Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚        Next.js Frontend         โ”‚
โ”‚  (Login โ†’ Verify โ†’ Chat Page)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
        โ”‚ HTTP     โ”‚ WebSocket (Socket.IO)
        โ–ผ          โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  User Serviceโ”‚  โ”‚   Chat Service   โ”‚
โ”‚  (Port 5000) โ”‚  โ”‚   (Port 5002)    โ”‚
โ”‚              โ”‚  โ”‚                  โ”‚
โ”‚  MongoDB     โ”‚  โ”‚  MongoDB         โ”‚
โ”‚  Redis       โ”‚  โ”‚  Socket.IO       โ”‚
โ”‚  RabbitMQ    โ”‚  โ”‚  Cloudinary      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚ publishes to queue
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Mail Serviceโ”‚  โ† consumes RabbitMQ queue
โ”‚  (Port 3001) โ”‚     sends OTP via Gmail SMTP
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The Chat Service makes internal HTTP calls to the User Service (via axios) to fetch user profile data when loading chats and messages.


๐Ÿ“ Project Folder Structure

Chat App/
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ user/                    # User Service (Port 5000)
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ”œโ”€โ”€ index.ts         # Express app entry, Redis + RabbitMQ setup
โ”‚   โ”‚       โ”œโ”€โ”€ config/
โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ db.ts        # MongoDB connection
โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ rabbitmq.ts  # RabbitMQ publisher
โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ generateToken.ts  # JWT generator
โ”‚   โ”‚       โ”‚   โ””โ”€โ”€ tryCatch.ts  # Global error wrapper
โ”‚   โ”‚       โ”œโ”€โ”€ model/user.ts    # User schema (name, email)
โ”‚   โ”‚       โ”œโ”€โ”€ controllers/user.ts  # loginUser, verifyUser, myProfile, updateName, ...
โ”‚   โ”‚       โ”œโ”€โ”€ middleware/isauth.ts # JWT auth middleware
โ”‚   โ”‚       โ””โ”€โ”€ routes/user.ts   # Route definitions
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ chat/                    # Chat Service (Port 5002)
โ”‚   โ”‚   โ””โ”€โ”€ src/
โ”‚   โ”‚       โ”œโ”€โ”€ index.ts         # Express app entry
โ”‚   โ”‚       โ”œโ”€โ”€ config/
โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ db.ts        # MongoDB connection
โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ socket.ts    # Socket.IO server setup
โ”‚   โ”‚       โ”‚   โ””โ”€โ”€ cloudinary.ts # Cloudinary config
โ”‚   โ”‚       โ”œโ”€โ”€ models/
โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ chat.ts      # Chat schema (users[], latestMessage)
โ”‚   โ”‚       โ”‚   โ””โ”€โ”€ messages.ts  # Message schema (text/image, seen, seenAt)
โ”‚   โ”‚       โ”œโ”€โ”€ controllers/chat.ts  # createNewChat, getAllChats, sendMessage, getMessagesByChat
โ”‚   โ”‚       โ”œโ”€โ”€ middlewares/
โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ isAuth.ts    # JWT auth middleware
โ”‚   โ”‚       โ”‚   โ””โ”€โ”€ multer.ts    # Cloudinary-backed file upload
โ”‚   โ”‚       โ””โ”€โ”€ routes/chat.ts   # Route definitions
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ mail/                    # Mail Service (Port 3001)
โ”‚       โ””โ”€โ”€ src/
โ”‚           โ”œโ”€โ”€ index.ts         # Express app entry
โ”‚           โ””โ”€โ”€ consumer.ts      # RabbitMQ consumer โ†’ Nodemailer email sender
โ”‚
โ””โ”€โ”€ frontend/                    # Next.js App
    โ””โ”€โ”€ app/
        โ”œโ”€โ”€ layout.tsx           # Root layout (AppProvider + SocketProvider)
        โ”œโ”€โ”€ page.tsx             # Root redirect
        โ”œโ”€โ”€ login/page.tsx       # Email input page
        โ”œโ”€โ”€ verify/page.tsx      # OTP verification page
        โ”œโ”€โ”€ chat/page.tsx        # Main chat interface
        โ”œโ”€โ”€ profile/page.tsx     # Profile/name edit page
        โ”œโ”€โ”€ context/
        โ”‚   โ”œโ”€โ”€ appContext.tsx   # Global state (user, chats, auth)
        โ”‚   โ””โ”€โ”€ SocketContext.tsx # Socket.IO client provider
        โ””โ”€โ”€ components/
            โ”œโ”€โ”€ ChatSidebar.tsx  # Chat list + user list panel
            โ”œโ”€โ”€ ChatHeader.tsx   # Active chat header + typing indicator
            โ”œโ”€โ”€ ChatMessages.tsx # Message bubbles with seen receipts
            โ”œโ”€โ”€ MessageInput.tsx # Text + image upload input
            โ”œโ”€โ”€ verifyOtp.tsx    # 6-digit OTP input logic
            โ””โ”€โ”€ Loading.tsx      # Loading spinner

โš™๏ธ Installation

Prerequisites

  • Node.js โ‰ฅ 18
  • Docker (for RabbitMQ)
  • MongoDB (Atlas or local)
  • Redis (Upstash or local)
  • Cloudinary account
  • Gmail account (with App Password)

Step 1 โ€” Clone the repo

git clone https://github.com/theUtkarshRaj/RelayChat.git
cd "Chat App"

Step 2 โ€” Start RabbitMQ via Docker

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-management

Step 3 โ€” Install dependencies

# User Service
cd backend/user && npm install

# Chat Service
cd ../chat && npm install

# Mail Service
cd ../mail && npm install

# Frontend
cd ../../frontend && npm install

๐Ÿ” Environment Variables

backend/user/.env

PORT=5000
MONGO_URI=mongodb+srv://<user>:<pass>@cluster.mongodb.net/
REDIS_URL=redis://<upstash-url>
JWT_SECRET=your_jwt_secret
Rabbitmq_Host=localhost
Rabbitmq_Username=admin
Rabbitmq_Password=admin123

backend/chat/.env

PORT=5002
MONGO_URI=mongodb+srv://<user>:<pass>@cluster.mongodb.net/
JWT_SECRET=your_jwt_secret
USER_SERVICE=http://localhost:5000
CLOUD_NAME=your_cloudinary_cloud_name
API_KEY=your_cloudinary_api_key
API_SECRET=your_cloudinary_api_secret

backend/mail/.env

port=3001
Rabbitmq_Host=localhost
Rabbitmq_Username=admin
Rabbitmq_Password=admin123
USER=your_gmail@gmail.com
PASSWORD=your_gmail_app_password

โš ๏ธ Use a Gmail App Password (not your regular Gmail password). Enable 2FA on your Google account and generate an App Password.


๐Ÿš€ Running the Project

Open 4 terminals simultaneously:

# Terminal 1 โ€” User Service
cd backend/user
npm run dev

# Terminal 2 โ€” Chat Service
cd backend/chat
npm run dev

# Terminal 3 โ€” Mail Service
cd backend/mail
npm run dev

# Terminal 4 โ€” Frontend
cd frontend
npm run dev

Visit: http://localhost:3000


๐Ÿ“ก API Overview

User Service (localhost:5000/api/v1)

Method Route Auth Description
POST /login โŒ Send OTP to email
POST /verify โŒ Verify OTP, return JWT + user
GET /me โœ… Get logged-in user profile
GET /user/all โœ… Get all users
GET /user/:id โŒ Get single user by ID
POST /update/user โœ… Update display name

Chat Service (localhost:5002/api/v1)

Method Route Auth Description
POST /chat/new โœ… Create or fetch a 1-to-1 chat
GET /chat/all โœ… Get all chats for logged-in user
POST /message โœ… Send a text or image message
GET /message/:chatId โœ… Get all messages in a chat

Socket.IO Events (Chat Service, Port 5002)

Event (emit) Direction Description
joinChat Client โ†’ Server Join a chat room
leaveChat Client โ†’ Server Leave a chat room
typing Client โ†’ Server Notify typing started
stopTyping Client โ†’ Server Notify typing stopped
newMessage Server โ†’ Client Broadcast new message
messagesSeen Server โ†’ Client Notify messages marked seen
getOnlineUser Server โ†’ Client Broadcast online users list
userTyping Server โ†’ Client Broadcast typing event

๐Ÿ–ผ๏ธ UI Description

  • Login Page โ€” Dark themed, email-only input with a blue submit button to send OTP
  • Verify Page โ€” 6 individual OTP digit boxes with auto-focus, paste support, and a 60-second resend timer
  • Chat Page โ€” Two-panel layout: left sidebar (chats / all-users toggle) and right message area with header, message bubbles, and input bar
  • Profile Page โ€” Shows current name with an edit form to update it

๐Ÿ”ฎ Future Improvements

  • Group chat support
  • Voice / video calling (WebRTC)
  • Push notifications (FCM)
  • Message deletion / editing
  • End-to-end encryption
  • Docker Compose for one-command startup
  • CI/CD pipeline
  • Profile picture upload
  • Search messages

๐Ÿ‘ค Author

Utkarsh Raj GitHub: @theUtkarshRaj

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages