A real-time chat application built with MongoDB, Express.js, React, and Node.js, featuring user authentication, real-time messaging, and file sharing capabilities.
- π User Authentication: Sign up and login with email, username, or mobile number
- π¬ Real-time Chat: Instant messaging using Socket.IO
- π₯ User Search: Find users by username or email
- π File Sharing: Upload and share images and videos
- π¨ Modern UI: Clean and responsive design with Tailwind CSS
- π JWT Authentication: Secure token-based authentication
- π± Responsive Design: Works on desktop and mobile devices
- Node.js - Runtime environment
- Express.js - Web framework
- MongoDB - Database
- Mongoose - ODM for MongoDB
- Socket.IO - Real-time communication
- JWT - Authentication
- Multer - File upload handling
- bcrypt - Password hashing
- React - UI library
- React Router - Client-side routing
- Socket.IO Client - Real-time communication
- Axios - HTTP client
- Tailwind CSS - Styling
- Node.js (v14 or higher)
- MongoDB (local installation or MongoDB Atlas)
- npm or yarn
git clone <repository-url>
cd CHAT\ APP
cd backend
npm install
Create a .env
file in the backend directory:
JWT_SECRET=your_super_secret_jwt_key_here_change_this_in_production
MONGO_URI=mongodb://127.0.0.1:27017/chat-app
PORT=3000
FRONTEND_URL=http://localhost:5173
cd ../frontend
npm install
Make sure MongoDB is running on your system:
# For local MongoDB
mongod
# Or start MongoDB service
sudo systemctl start mongod
- Start the backend server:
cd backend
npm run dev
- Start the frontend development server:
cd frontend
npm run dev
- Open your browser:
- Frontend: http://localhost:5173
- Backend API: http://localhost:3000
- Build the frontend:
cd frontend
npm run build
- Start the backend:
cd backend
npm start
POST /api/auth/signup
- User registrationPOST /api/auth/login
- User loginPOST /api/auth/logout
- User logout
GET /api/chat/messages/:userId
- Get messages between usersGET /api/chat/chats
- Get all chats for current userGET /api/chat/search
- Search usersPOST /api/chat/upload
- Upload files
GET /api/users
- Get all users
private_message
- Send a private messageconnection
- User connects
private_message
- Receive a private messagemessage_sent
- Message sent confirmationerror
- Error occurred
The application supports uploading:
- Images: JPEG, PNG, GIF
- Videos: MP4, WebM, MOV
- Maximum file size: 10MB per file
Files are stored in the backend/uploads
directory and served statically.
{
name: String (required),
username: String (required, unique),
email: String (required, unique),
mobile: String (optional, unique),
password: String (hashed),
avatar: String (optional),
isOnline: Boolean,
lastSeen: Date,
createdAt: Date
}
{
chatId: String (required),
sender: ObjectId (ref: User),
receiver: ObjectId (ref: User),
text: String,
attachments: [{
filename: String,
url: String,
mimeType: String,
size: Number
}],
createdAt: Date,
edited: Boolean,
deleted: Boolean
}
CHAT APP/
βββ backend/
β βββ controllers/
β β βββ authcontroller.js
β βββ middleware/
β β βββ auth.js
β β βββ socketAuth.js
β βββ models/
β β βββ User.js
β β βββ MessageModel.js
β βββ routes/
β β βββ auth.js
β β βββ chat.js
β β βββ user.js
β βββ uploads/ (created automatically)
β βββ index.js
β βββ package.json
βββ frontend/
β βββ src/
β β βββ components/
β β β βββ Chat.jsx
β β β βββ Login.jsx
β β β βββ MessageInput.jsx
β β β βββ MessageList.jsx
β β β βββ Signup.jsx
β β β βββ UserList.jsx
β β βββ context/
β β β βββ AuthContext.jsx
β β βββ App.jsx
β β βββ main.jsx
β β βββ index.css
β βββ package.json
βββ README.md
JWT_SECRET
- Secret key for JWT tokensMONGO_URI
- MongoDB connection stringPORT
- Server port (default: 3000)FRONTEND_URL
- Frontend URL for CORS
-
MongoDB Connection Error
- Ensure MongoDB is running
- Check the connection string in
.env
- Verify MongoDB is accessible on the specified port
-
Socket.IO Connection Issues
- Check if backend server is running
- Verify CORS settings
- Ensure token is properly set in localStorage
-
File Upload Issues
- Check uploads directory permissions
- Verify file size limits
- Ensure supported file types
-
Authentication Issues
- Check JWT_SECRET in environment variables
- Verify token expiration
- Clear localStorage if needed
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License.
- Message encryption
- Group chats
- Voice messages
- Video calls
- Message reactions
- Push notifications
- Dark mode
- Message search
- User presence indicators
- Message read receipts