Skip to content
kul-91Public

About

this is a real world project

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💬 Real-Time Chat Application

A full-stack real-time chat application built with the MERN ecosystem. The project provides secure user authentication, one-to-one messaging, real-time message delivery, image sharing, profile customization, and a responsive modern interface.

The application is designed as a practical full-stack project demonstrating how a React frontend communicates with a Node.js/Express backend, how authentication is handled with JWT, how chat data is stored in MongoDB, and how real-time communication is implemented using Socket.IO.


✨ Features

🔐 Authentication & Authorization

  • User registration and login
  • Password hashing using bcrypt
  • JWT-based authentication
  • JWT stored in HTTP cookies
  • Protected routes using authentication middleware
  • Logout functionality
  • Persistent authentication state

💬 Real-Time Messaging

  • One-to-one private conversations
  • Send and receive messages in real time
  • Message history stored in MongoDB
  • Real-time message delivery using Socket.IO
  • Automatically identifies connected users
  • Sidebar displaying available users for chatting

🖼️ Image Sharing

  • Send images inside conversations
  • Images are uploaded to Cloudinary
  • Secure Cloudinary URLs are stored with messages
  • Profile pictures can also be uploaded and updated through Cloudinary

👤 User Profiles

  • User profile page
  • Custom profile picture
  • User information stored in MongoDB
  • Profile updates for authenticated users

🎨 Modern Frontend

  • Built with React

  • Responsive chat interface

  • Tailwind CSS styling

  • DaisyUI components

  • Lucide icons

  • Toast notifications

  • Multiple application pages including:

    • Login
    • Sign Up
    • Home / Chat
    • Profile
    • Settings

⚡ State Management

Application state is managed using Zustand, with separate stores for:

  • Authentication
  • Chat state
  • Theme/settings

🛠️ Tech Stack

Frontend

Technology Purpose
React UI development
Vite Frontend development/build tool
React Router Client-side routing
Tailwind CSS Styling
DaisyUI UI components
Zustand State management
Axios API requests
Socket.IO Client Real-time communication
React Hot Toast Notifications
Lucide React Icons

Backend

Technology Purpose
Node.js Runtime
Express.js REST API
MongoDB Database
Mongoose MongoDB ODM
Socket.IO Real-time communication
JWT Authentication
bcryptjs Password hashing
Cloudinary Image storage
Cookie Parser Cookie handling
CORS Cross-origin requests
dotenv Environment variables
Nodemon Development server

The backend dependencies and architecture are organized around controllers, models, routes, middleware and supporting libraries.


🏗️ Architecture

                         ┌─────────────────────┐
                         │      React UI       │
                         │                     │
                         │ React + Vite        │
                         │ Tailwind + DaisyUI  │
                         │ Zustand             │
                         └──────────┬──────────┘
                                    │
                       HTTP / REST API
                                    │
                                    ▼
                         ┌─────────────────────┐
                         │   Express Server    │
                         │                     │
                         │ Authentication      │
                         │ Message APIs        │
                         │ Middleware          │
                         └──────────┬──────────┘
                                    │
                  ┌─────────────────┼─────────────────┐
                  │                 │                 │
                  ▼                 ▼                 ▼
             MongoDB           Socket.IO         Cloudinary
             Database          Real-time          Image Storage
                                Events

How messaging works

User A
  │
  │ Send message
  ▼
React Frontend
  │
  │ POST request
  ▼
Express API
  │
  ├── Authenticate user
  │
  ├── Save message
  │       │
  │       ▼
  │    MongoDB
  │
  └── Socket.IO
          │
          │ newMessage event
          ▼
       User B

When a message is sent, the backend stores it in MongoDB and uses Socket.IO to immediately emit the new message to the recipient if they are connected.


📁 Project Structure

chat-app/
│
├── backend/
│   ├── src/
│   │   ├── controllers/
│   │   │   ├── auth.controllers.js
│   │   │   └── message.controller.js
│   │   │
│   │   ├── lib/
│   │   │   ├── cloudinary.js
│   │   │   ├── db.js
│   │   │   ├── socket.js
│   │   │   └── utils.js
│   │   │
│   │   ├── middleware/
│   │   │   └── auth.middleware.js
│   │   │
│   │   ├── models/
│   │   │   ├── user.model.js
│   │   │   └── message.model.js
│   │   │
│   │   ├── routes/
│   │   │   ├── auth.route.js
│   │   │   └── message.route.js
│   │   │
│   │   └── index.js
│   │
│   └── package.json
│
├── frontend/
│   ├── src/
│   │   ├── components/
│   │   ├── constants/
│   │   ├── lib/
│   │   ├── pages/
│   │   ├── store/
│   │   ├── App.jsx
│   │   ├── index.css
│   │   └── main.jsx
│   │
│   └── package.json
│
├── package.json
└── README.md

The repository follows a clear frontend/backend separation, while the backend itself is divided into controllers, models, routes, middleware and supporting libraries.


🚀 Getting Started

Prerequisites

Before running the project, make sure you have:

  • Node.js installed
  • npm installed
  • A MongoDB database
  • A Cloudinary account
  • Git

1. Clone the Repository

git clone https://github.com/kul-91/chat-app.git
cd chat-app

2. Install Dependencies

You can install the frontend and backend dependencies from the root directory:

npm run build

The root build script installs dependencies for both the backend and frontend and then builds the frontend.

Alternatively, install them separately:

Backend

cd backend
npm install

Frontend

cd ../frontend
npm install

🔑 Environment Variables

Create a .env file inside the backend directory.

PORT=5001

MONGODB_URI=your_mongodb_connection_string

JWT_SECRET=your_jwt_secret

CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secret

Important: Never commit your .env file or expose your API keys and secrets publicly.


▶️ Running the Application

Start the Backend

Open a terminal:

cd backend
npm run dev

The backend development server uses Nodemon for automatic restarts.


Start the Frontend

Open another terminal:

cd frontend
npm run dev

Vite will start the frontend development server.

Open the URL displayed by Vite in your browser.


🔄 Available Scripts

Root

npm run build

Installs backend and frontend dependencies and creates the frontend production build.

npm start

Starts the backend application.

Backend

npm run dev

Starts the backend using Nodemon.

npm start

Starts the backend using Node.js.

Frontend

npm run dev

Starts the Vite development server.

npm run build

Creates a production build.

npm run preview

Previews the production build.

npm run lint

Runs ESLint.


🔐 Authentication Flow

The application uses JWT-based authentication.

Sign Up / Login
       │
       ▼
 Express Authentication API
       │
       ├── Validate credentials
       │
       ├── Hash / compare password
       │
       └── Generate JWT
                │
                ▼
             Cookie
                │
                ▼
       Protected API Requests
                │
                ▼
       Authentication Middleware
                │
                ▼
           req.user

Passwords are hashed with bcrypt before being stored, while JWT tokens are generated during authentication and stored through cookies.


💬 Messaging Flow

Users can select another user from the chat sidebar and start a private conversation.

Messages support:

  • Text
  • Images

When an image is sent, it is uploaded to Cloudinary and the resulting secure URL is associated with the message.

Real-time delivery is handled using Socket.IO:

Sender
  │
  │ message
  ▼
Backend
  │
  ├── Save → MongoDB
  │
  └── Emit → Socket.IO
                 │
                 ▼
              Receiver

🗄️ Database

MongoDB is used as the primary database.

The project currently uses two main models:

User

Stores user information such as:

  • Name
  • Email
  • Password hash
  • Profile picture

Message

Stores conversation information such as:

  • Sender
  • Receiver
  • Text
  • Image
  • Message data

The backend uses Mongoose to interact with MongoDB.


📡 API Overview

Authentication

POST   /api/auth/signup
POST   /api/auth/login
POST   /api/auth/logout
PUT    /api/auth/update-profile
GET    /api/auth/check

Messages

GET    /api/messages/users
GET    /api/messages/:id
POST   /api/messages/send/:id

The exact API base paths should be checked against the route configuration if you modify the backend routing structure.


🧩 Key Concepts Demonstrated

This project is useful for learning how a production-style full-stack application is structured.

It demonstrates:

  • REST API development
  • Authentication and authorization
  • JWT authentication
  • HTTP cookies
  • Password hashing
  • MongoDB data modeling
  • Mongoose
  • Express middleware
  • React component architecture
  • Client-side routing
  • Global state management
  • Real-time communication with WebSockets
  • Socket.IO
  • Image upload and cloud storage
  • Environment variable management
  • Frontend/backend separation
  • API integration using Axios

🧪 Future Improvements

Some possible improvements for future versions:

  • Group chats
  • Message read receipts
  • Typing indicators
  • Online/offline status
  • Message reactions
  • Message editing and deletion
  • File/document sharing
  • Voice messages
  • Video calling
  • Push notifications
  • Message search
  • Pagination / infinite scrolling
  • End-to-end encryption
  • Deployment with CI/CD
  • Automated tests

🤝 Contributing

Contributions are welcome.

  1. Fork the repository
  2. Create a feature branch
git checkout -b feature/your-feature
  1. Make your changes
  2. Commit your changes
git commit -m "Add your feature"
  1. Push the branch
git push origin feature/your-feature
  1. Open a Pull Request

📄 License

This project is currently licensed under the ISC License.


👨‍💻 Author

Kuldeep

GitHub: @kul-91


⭐ If you find this project useful, consider giving the repository a star!

View Repository

About

this is a real world project

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages