A real-time chat application built with Node.js, Express, Socket.IO, and MongoDB, inspired by WhatsApp's design and functionality.
- Name: Zaid Haider
- Email: zaidhaider9@gmail.com
- GitHub: @zaidhaider9
- Real-time messaging
- User authentication (login/register)
- Online/offline status
- Profile management (photo and name)
- Chat history
- Message status indicators
- Responsive design
-
Backend:
- Node.js
- Express.js
- Socket.IO
- MongoDB
- Mongoose
- Express-session
- Bcrypt
- Formidable
-
Frontend:
- HTML5
- CSS3
- JavaScript
- EJS (Embedded JavaScript)
- Socket.IO Client
- Node.js (v14 or higher)
- MongoDB (v4.4 or higher)
- npm or yarn
- Clone the repository:
git clone https://github.com/yourusername/realtime-chat-app.git
cd realtime-chat-app
- Install dependencies:
npm install
- Create a
.env
file in the root directory:
MONGODB_URI=mongodb://localhost:27017/chat-app
SESSION_SECRET=your-secret-key
PORT=3000
- Start the server:
npm start
- Open your browser and navigate to:
http://localhost:3000
realtime-chat-app/
├── public/
│ ├── css/
│ │ └── style.css
│ ├── images/
│ └── views/
│ ├── index.ejs
│ ├── login.ejs
│ └── register.ejs
├── src/
│ ├── models/
│ │ ├── User.js
│ │ └── Chat.js
│ └── server.js
├── .env
├── package.json
└── README.md
- Endpoint:
POST /register
- Request Body:
{
"username": "string",
"password": "string",
"name": "string"
}
- Response:
{
"success": true,
"user": {
"_id": "string",
"username": "string",
"name": "string",
"profilePhoto": "string"
}
}
- Endpoint:
POST /login
- Request Body:
{
"username": "string",
"password": "string"
}
- Response:
{
"success": true,
"user": {
"_id": "string",
"username": "string",
"name": "string",
"profilePhoto": "string"
}
}
- Endpoint:
POST /profile/photo
- Request: FormData with 'photo' file
- Response:
{
"success": true,
"photo": "string"
}
- Endpoint:
POST /profile/name
- Request Body:
{
"name": "string"
}
- Response:
{
"success": true,
"name": "string"
}
set user
: Set current user IDuser online
: Notify user is onlineget chat
: Get or create chat with userchat message
: Send message in chatcreate chat
: Create new chat
online users
: List of online userschat found
: Chat detailschat created
: New chat createdchat message
: New message receivedupdate chat list
: Update chat list with last messageerror
: Error notification
- User Registration
// Test successful registration
const testUser = {
username: "testuser",
password: "password123",
name: "Test User"
};
// Expected: Success response with user details
// Test duplicate username
const duplicateUser = {
username: "testuser",
password: "password123",
name: "Duplicate User"
};
// Expected: Error response
- User Login
// Test successful login
const loginCredentials = {
username: "testuser",
password: "password123"
};
// Expected: Success response with user details
// Test invalid credentials
const invalidCredentials = {
username: "testuser",
password: "wrongpassword"
};
// Expected: Error response
- Chat Creation
// Test creating new chat
const chatData = {
participants: ["user1", "user2"]
};
// Expected: New chat created with both participants
// Test existing chat retrieval
const existingChat = {
participants: ["user1", "user2"]
};
// Expected: Existing chat returned
- Message Sending
// Test sending message
const message = {
chatId: "chat123",
content: "Hello, World!"
};
// Expected: Message saved and broadcasted to participants
// Test empty message
const emptyMessage = {
chatId: "chat123",
content: ""
};
// Expected: Error response
- Profile Photo Update
// Test photo upload
const formData = new FormData();
formData.append('photo', imageFile);
// Expected: Photo uploaded and URL returned
// Test invalid file type
const invalidFile = new FormData();
invalidFile.append('photo', textFile);
// Expected: Error response
- Profile Name Update
// Test name update
const nameUpdate = {
name: "New Name"
};
// Expected: Name updated successfully
// Test empty name
const emptyName = {
name: ""
};
// Expected: Error response
The application handles various types of errors:
-
Authentication Errors
- Invalid credentials
- Duplicate username
- Session expiration
-
Chat Errors
- Invalid chat ID
- User not in chat
- Empty messages
-
Profile Errors
- Invalid file type
- File size too large
- Empty name
-
Password Security
- Passwords are hashed using bcrypt
- No plain text passwords stored
-
Session Management
- Secure session handling with express-session
- Session data stored in MongoDB
-
File Upload Security
- File type validation
- File size limits
- Secure file storage
-
Database Optimization
- Indexed fields for faster queries
- Efficient data models
-
Real-time Updates
- Socket.IO rooms for targeted updates
- Efficient message broadcasting
-
Client-side Optimization
- Lazy loading of chat history
- Efficient DOM updates
-
Features to Add
- Group chats
- Message reactions
- File sharing
- Voice messages
- Video calls
-
Improvements
- Message encryption
- Better error handling
- Performance optimization
- UI/UX enhancements
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
This project is licensed under the MIT License.
Copyright (c) 2024 Zaid Haider
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.