Skip to content

πŸ’¬ A real-time chat application backend using Node.js, Socket.IO, and MongoDB. Supports instant messaging, user auth, and scalable room handling.

buildwithmehul/realtime-chat-backend-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’¬ Real-Time Chat App Backend (Socket.IO + Express + MongoDB)

Welcome to the backend of a full-fledged real-time chat system β€” built from scratch using Node.js, Express, MongoDB, and Socket.IO πŸš€

πŸ“’ From real-time messaging, offline delivery, read receipts, profile editing, admin tools, and even a chatbot β€” this backend is loaded.
βœ… Tested purely with Postman & Socket.IO Client Tool β€” no frontend needed.


βš™οΈ Tech Stack

  • Node.js + Express – API and server logic
  • MongoDB + Mongoose – Database and models
  • Socket.IO – Real-time messaging, delivery, typing, read events
  • Postman + Socket.IO Client Tool – API and event testing
  • JWT Auth – Secure user endpoints
  • Nodemon – Auto-reload during dev

✨ Features

βœ… Auth (JWT)

  • POST /register – New user registration
  • POST /login – Login and receive JWT token
  • JWT-protected routes for profile and admin

βœ… Real-Time Chat Engine

πŸ”Œ register

  • On socket connect, emit register
  • Backend:
    • Stores username ↔ socketId in memory
    • Marks user as online
    • Pushes undelivered messages

πŸ’¬ sendMessage

  • Instantly delivers if receiver online
  • Saves to DB regardless
  • Handles delivered status

πŸ“€ Offline Delivery

  • When a user connects:
    • All delivered: false messages are pushed
    • Status updated in DB

πŸ‘οΈ messageSeen

  • Mark messages as seen: true
  • Notifies original sender via seenAck event

✍️ Typing Indicators

  • typing and stop-typing events supported

πŸ“‘ Online Status

  • Emits onlineStatus updates to all users
  • GET /api/users/online returns list of active users

πŸ€– Auto-Reply Bot (Fun Feature)

  • Send messages to Bot like:
    • "hello", "who are you", "joke", "bye"
  • Bot responds with hardcoded replies

πŸ” Admin Panel Features

JWT role-based admin with these routes:

Route Description
DELETE /admin/users/:id Delete any user
DELETE /admin/messages/:id Delete any message
POST /admin/freeze/:id Freeze a user (disable sending)
POST /admin/unfreeze/:id Unfreeze a user

🧾 Profile Management

  • GET /api/users/profile/me – Get your profile
  • PUT /api/users/profile/me – Edit bio or avatar
  • GET /api/users/profile/:username – View others' profiles
  • GET /api/users/ – List all users (except self)

πŸ“Š Analytics & Export

  • GET /api/messages/analytics
    β†’ Returns:

    {
      "totalMessages": 145,
      "delivered": 140,
      "seen": 100,
      "undelivered": 5
    }
  • GET /api/messages/export
    β†’ Download all your messages in .txt format


πŸ§ͺ Testing Instructions

Socket.IO Client Tool:

  • Emit register β†’ "mehul"
  • Emit sendMessage β†’ { sender, receiver, message }
  • Emit messageSeen β†’ { sender, receiver }
  • Emit typing β†’ { to }, then stop-typing

Postman:

  • Auth: POST /register, POST /login
  • Protected routes (use Bearer token in header):
    • GET /api/users/
    • GET /api/users/profile/me
    • PUT /api/users/profile/me
    • GET /api/messages/history?user1=A&user2=B
    • Admin routes (/admin/...)
    • Bot via regular sendMessage with receiver: "Bot"

πŸ—‚οΈ Project Structure

chat-backend/
β”œβ”€β”€ server.js
β”œβ”€β”€ config/
β”‚   └── db.js
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ User.js
β”‚   └── Message.js
β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ messageRoutes.js
β”‚   β”œβ”€β”€ userRoutes.js
β”‚   β”œβ”€β”€ statusRoutes.js
β”‚   β”œβ”€β”€ adminRoutes.js
β”‚   └── onlineRoute.js
β”œβ”€β”€ sockets/
β”‚   └── chat.js
β”œβ”€β”€ utils/
β”‚   └── userStore.js
β”œβ”€β”€ middleware/
β”‚   └── auth.js
β”œβ”€β”€ .env
β”œβ”€β”€ package.json
└── README.md

πŸ§‘β€πŸ’» Author

Mehul Khanna
Backend Developer Intern Β· 2025
Passionate about building real-time scalable systems πŸ’»πŸ”₯


⭐️ If you like this project

Don’t forget to star ⭐ the repo and fork if you'd like to build on top of it!

About

πŸ’¬ A real-time chat application backend using Node.js, Socket.IO, and MongoDB. Supports instant messaging, user auth, and scalable room handling.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published