Skip to content

Repository files navigation

ChatNest

A real-time chat application where users can connect with friends and message each other instantly, with support for one-on-one and group conversations — similar in spirit to WhatsApp.


Overview

ChatNest lets users find and add friends, then chat with them in real time with instant delivery, typing indicators, online/offline presence, and read receipts. Beyond one-on-one messaging, users can create groups with admins and members, share media (images, videos, PDFs), and personalize their chat experience with custom wallpapers.

What makes the messaging layer interesting under the hood is how it balances real-time speed with database efficiency: active conversations are held in Redis in memory and only flushed to MongoDB in batches — either when a chat room becomes inactive or a user leaves — instead of writing to the database on every single message. This keeps the chat experience fast while avoiding unnecessary database load.


Key Features

  • Real-time one-on-one and group messaging via Socket.IO
  • Friend system — send/accept friend requests and explore/discover other users
  • Group chats — create groups with dedicated admins and members
  • Media sharing — send images, videos, and PDFs (via Cloudinary)
  • Typing indicators — see when the other person is typing
  • Online/offline presence — real-time status updates broadcast to all connected users
  • Read receipts — know when your message has been seen
  • Redis-backed message buffering — active chats are held in Redis and batch-written to MongoDB for performance, rather than hitting the database on every message
  • User preferences — customizable chat wallpaper per user
  • Notification sounds — audio cues for sent/received messages and alerts
  • JWT-based authentication with secure password hashing

How Real-Time Messaging Works

  1. When a user opens a chat, their socket joins a room (either a private 1:1 room or a group room).
  2. Redis tracks which sockets belong to which room and how many participants are currently active in it.
  3. As messages are sent, they're appended to Redis rather than written straight to MongoDB — this keeps message delivery fast since it avoids a database write on every keystroke-driven event.
  4. Once a room is left, or drops back down to a single active participant, the buffered messages in Redis for that room are flushed to MongoDB in one batch and the Redis cache for that room is cleared.
  5. All users also join a global room on connection, used to broadcast presence updates (online/offline) across the app in real time.

Tech Stack

Layer Technology
Framework Next.js (App Router), custom Node.js server
Real-time Socket.IO
Database MongoDB (Mongoose)
Caching / Message Buffer Redis (ioredis)
Media Storage Cloudinary
Auth JWT, bcrypt
State Management Redux Toolkit
UI Radix UI, Tailwind CSS, React Hook Form, Zod
Containerization Docker, Docker Compose

Project Structure

src/
├── app/
│   ├── (auth)/          # Login & signup pages
│   ├── (main)/           # Main app — chat, groups, explore, profile
│   └── api/v1/             # REST endpoints — users, friends, groups, chats, preferences
├── models/               # Mongoose schemas — User, Chat, Friend, Group, GroupChat, Preferences
├── redux/                # Redux Toolkit store & slices (user, preferences)
├── helpers/              # DB connection, Cloudinary, socket client, auth helpers
├── components/           # Reusable UI components
└── types/                # TypeScript types

Server.js         # Custom Node server — wires up Next.js with Socket.IO
RedisClient.js     # Redis helper — room/session/chat-buffer management

Getting Started

Prerequisites

  • Node.js
  • MongoDB
  • Redis
  • A Cloudinary account (for media uploads)

Installation

git clone https://github.com/mdrehan369/ChatNest.git
cd ChatNest
npm install

Environment Variables

Copy .env.sample to .env and fill in the required values (MongoDB URI, Redis connection, JWT secret, Cloudinary credentials, etc.).

cp .env.sample .env

Running Locally

npm run dev

This starts the custom server (Server.js), which runs Next.js and Socket.IO together on http://localhost:3000.

Running with Docker

docker compose up

Available Scripts

Script Description
npm run dev Start the development server (Next.js + Socket.IO)
npm run build Build the app for production
npm start Start the production server
npm run lint Run ESLint

Roadmap

  • Message search
  • Voice/video calling
  • End-to-end encryption
  • Message reactions

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to your branch
  5. Open a Pull Request

License

This project is licensed under the MIT License.


Author

MD Rehan GitHub · LinkedIn

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages