A full-stack real-time chat application built with the MERN stack (MongoDB, Express.js, React, Node.js) featuring real-time messaging, online user status, profile management, and image uploads.
- ๐ Authentication & Authorization - JWT-based secure authentication
- ๐ฌ Real-Time Messaging - Instant messaging using Socket.IO
- ๐ฅ Online User Status - See who's online in real-time
- ๐ผ๏ธ Image Uploads - Profile picture uploads with Cloudinary
- ๐จ Modern UI - Beautiful interface with Tailwind CSS and DaisyUI
- ๐ฑ Responsive Design - Works seamlessly on desktop and mobile
- ๐ Secure - HTTP-only cookies, password hashing with bcrypt
- โก Fast - Optimized with Vite and React 19
- ๐ญ Theme Support - Multiple theme options
- ๐ TypeScript - Type-safe frontend code
chat-app/
โโโ backend/ # Backend server
โ โโโ src/
โ โ โโโ controllers/ # Request handlers
โ โ โ โโโ auth.controller.js
โ โ โ โโโ message.controller.js
โ โ โโโ lib/ # Utility libraries
โ โ โ โโโ cloudinary.js # Image upload config
โ โ โ โโโ db.js # Database connection
โ โ โ โโโ socket.js # Socket.IO setup
โ โ โ โโโ utils.js # Helper functions
โ โ โโโ middleware/ # Custom middleware
โ โ โ โโโ auth.middleware.js
โ โ โโโ models/ # Database models
โ โ โ โโโ message.model.js
โ โ โ โโโ user.model.js
โ โ โโโ routes/ # API routes
โ โ โ โโโ auth.route.js
โ โ โ โโโ message.route.js
โ โ โโโ seeds/ # Database seeders
โ โ โ โโโ user.seed.js
โ โ โโโ index.js # Entry point
โ โโโ .env # Environment variables
โ โโโ package.json
โ
โโโ frontend/ # Frontend application
โ โโโ src/
โ โ โโโ components/ # React components
โ โ โ โโโ AuthImagePattern.tsx
โ โ โ โโโ ChatContainer.tsx
โ โ โ โโโ ChatHeader.tsx
โ โ โ โโโ Loader.tsx
โ โ โ โโโ MessageInput.tsx
โ โ โ โโโ Navbar.tsx
โ โ โ โโโ NoChatSelected.tsx
โ โ โ โโโ Sidebar.tsx
โ โ โ โโโ skeletons/ # Loading skeletons
โ โ โ โโโ MessageSkeleton.tsx
โ โ โ โโโ SidebarSkeleton.tsx
โ โ โโโ constants/ # App constants
โ โ โ โโโ index.ts
โ โ โโโ lib/ # Utilities
โ โ โ โโโ axios.ts # API client
โ โ โ โโโ utils.ts # Helper functions
โ โ โโโ pages/ # Page components
โ โ โ โโโ HomePage.tsx
โ โ โ โโโ LoginPage.tsx
โ โ โ โโโ ProfilePage.tsx
โ โ โ โโโ SettingsPage.tsx
โ โ โ โโโ SignUpPage.tsx
โ โ โโโ store/ # State management (Zustand)
โ โ โ โโโ useAuthStore.ts
โ โ โ โโโ useChatStore.ts
โ โ โ โโโ useThemeStore.ts
โ โ โโโ App.tsx # Main component
โ โ โโโ main.tsx # Entry point
โ โ โโโ index.css # Global styles
โ โโโ public/ # Static assets
โ โโโ .env # Environment variables
โ โโโ package.json
โ
โโโ .gitignore
โโโ package.json # Root package.json
โโโ README.md
โโโ SECURITY.md
- Node.js - Runtime environment
- Express.js - Web framework
- MongoDB - NoSQL database
- Mongoose - ODM for MongoDB
- Socket.IO - Real-time bidirectional communication
- JWT - Authentication tokens
- Bcrypt.js - Password hashing
- Cloudinary - Image storage and management
- Cookie-Parser - Cookie handling
- CORS - Cross-origin resource sharing
- Dotenv - Environment variable management
- React 19 - UI library
- TypeScript - Type safety
- Vite - Build tool and dev server
- Tailwind CSS - Utility-first CSS framework
- DaisyUI - Tailwind CSS component library
- Zustand - State management
- React Router DOM - Client-side routing
- Axios - HTTP client
- Socket.IO Client - Real-time communication
- React Hot Toast - Toast notifications
- Lucide React - Icon library
Before you begin, ensure you have the following installed:
- Node.js (v18.0.0 or higher)
- npm or yarn
- MongoDB (local installation or MongoDB Atlas account)
- Git
- Cloudinary Account (for image uploads)
git clone https://github.com/eshanhasitha/chat-app.git
cd chat-appcd backend
npm installCreate a .env file in the backend directory:
PORT=5001
MONGODB_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret_key
NODE_ENV=development
CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secretEnvironment Variables Explained:
PORT- Port number for the backend server (default: 5001)MONGODB_URI- MongoDB connection stringJWT_SECRET- Secret key for JWT token generation (use a strong random string)NODE_ENV- Environment mode (developmentorproduction)CLOUDINARY_*- Cloudinary credentials for image uploads
Generate a secure JWT secret:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"Development mode (with auto-restart):
npm run devProduction mode:
npm startThe backend server will run on http://localhost:5001
cd frontend
npm installIf your backend is running on a different URL, create a .env file in the frontend directory:
VITE_API_URL=http://localhost:5001/apiBy default, the frontend connects to http://localhost:5001/api
Development mode:
npm run devThe frontend application will run on http://localhost:5173
Build for production:
npm run buildPreview production build:
npm run previewNavigate to http://localhost:5173 in your browser to use the application.
- Click on "Sign Up" on the homepage
- Enter your full name, email, and password
- Click "Create Account"
- Click on "Login"
- Enter your registered email and password
- Click "Sign In"
- Select a user from the sidebar
- Type your message in the input field
- Press Enter or click the send button
- Messages appear in real-time for both users
- Click on the profile icon in the navbar
- Click "Edit Profile"
- Upload a new profile picture or update your information
- Changes are saved automatically
- Green dot indicates online users
- Users can see who is currently online in real-time
- Click the settings icon
- Choose from available themes
- Theme preference is saved locally
POST /api/auth/signup- Register a new userPOST /api/auth/login- Login userPOST /api/auth/logout- Logout userGET /api/auth/check- Check authentication statusPUT /api/auth/update-profile- Update user profile
GET /api/messages/users- Get all users for sidebarGET /api/messages/:id- Get messages with a specific userPOST /api/messages/send/:id- Send a message to a user
connection- User connectsdisconnect- User disconnectsnewMessage- New message sentgetOnlineUsers- Broadcast online users
- Password hashing with bcrypt
- JWT token-based authentication
- HTTP-only cookies for token storage
- CORS protection
- Input validation and sanitization
- Secure headers with Express
- Environment variable protection
{
email: String (unique, required),
fullName: String (required),
password: String (required, hashed),
profilePic: String (default: avatar),
createdAt: Date
}{
senderId: ObjectId (ref: User, required),
receiverId: ObjectId (ref: User, required),
text: String,
image: String,
createdAt: Date
}Error: Port 5001 is already in use
# Windows
netstat -ano | findstr :5001
taskkill /PID <PID> /F
# Mac/Linux
lsof -ti:5001 | xargs kill -9Error: MongoDB connection failed
- Check if MongoDB is running locally
- Verify MONGODB_URI in .env file
- For MongoDB Atlas, check network access settings
- Ensure backend is running on port 5001
- Check CORS settings in backend
- Verify VITE_API_URL in frontend .env
- Verify Cloudinary credentials in backend .env
- Check file size limits
- Ensure proper MIME types are allowed
From the root directory:
npm run build
npm startcd backend
npm install --production
NODE_ENV=production npm startcd frontend
npm run build
# Deploy the 'dist' folder to your hosting service- Push code to GitHub
- Connect repository to hosting platform
- Set environment variables
- Deploy
- Push code to GitHub
- Connect repository to hosting platform
- Set build command:
npm run build - Set output directory:
dist - Add environment variable:
VITE_API_URL - Deploy
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Socket.IO for real-time communication
- Cloudinary for image management
- MongoDB for database solution
- The React and Node.js communities