Real-time Chat Application
A full-stack real-time chat application built with Node.js, Express, MongoDB, React, and WebSockets. This application allows users to register, log in, and chat with other online users in real-time.
Table of Contents
Features
- User registration and authentication
- Real-time messaging with WebSockets
- Display of online and offline users
- Responsive design using Tailwind CSS
- Secure password hashing with bcrypt
- JWT-based authentication
- Persistent message history stored in MongoDB
Technologies Used
Backend
- Node.js
- Express.js
- MongoDB
- Mongoose
- WebSocket
- JSON Web Tokens (JWT)
- bcrypt.js
- dotenv
Frontend
- React.js
- Vite
- Tailwind CSS
- Axios
- Lodash
Prerequisites
- Node.js (v14 or higher)
- npm or Yarn
- MongoDB (running instance)
- Git (for cloning the repository)
Installation
Backend Setup
- Clone the Repository
git clone https://github.com/yourusername/your-repo-name.git
cd your-repo-name/api
- Install Dependencies
Using Yarn:
yarn install
Or using npm:
npm install
- Configure Environment Variables
Create a .env file in the api directory with the following content:
MONGO_URL=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret_key
CLIENT_URL=http://localhost:5173
- Replace
your_mongodb_connection_stringwith your MongoDB connection string. - Replace
your_jwt_secret_keywith a secure secret key.
- Start the Backend Server
yarn start
The backend server will start on http://localhost:4040.
Frontend Setup
- Navigate to the Client Directory
cd ../client
- Install Dependencies
Using Yarn:
yarn install
Or using npm:
npm install
- Start the Frontend Development Server
yarn dev
The frontend will start on http://localhost:5173.
Usage
- Open your browser and navigate to
http://localhost:5173. - Register a new user account.
- Log in with your credentials.
- You will see a list of online users on the sidebar.
- Click on a user to start chatting in real-time.
Project Structure
project-root/
├── api/
│ ├── models/
│ │ ├── Message.js
│ │ └── User.js
│ ├── .env
│ ├── index.js
│ ├── package.json
│ └── yarn.lock
├── client/
│ ├── public/
│ ├── src/
│ │ ├── App.jsx
│ │ ├── Avatar.jsx
│ │ ├── Chat.jsx
│ │ ├── Contact.jsx
│ │ ├── Logo.jsx
│ │ ├── Main.jsx
│ │ ├── RegisterAndLoginForm.jsx
│ │ ├── Routes.jsx
│ │ └── UserContext.jsx
│ ├── .gitignore
│ ├── index.html
│ ├── package.json
│ ├── postcss.config.cjs
│ ├── tailwind.config.cjs
│ ├── vite.config.js
│ └── yarn.lock
├── .gitignore
└── README.md
Key Components
Backend (
apidirectory)index.js: Main server file that sets up Express, WebSocket server, and handles routes.models/User.js: Mongoose model for user data.models/Message.js: Mongoose model for message data.
Frontend (
clientdirectory)src/App.jsx: Root component that sets up Axios defaults and wraps the app withUserContextProvider.src/Routes.jsx: Handles routing based on user authentication state.src/UserContext.jsx: Context provider for managing user state across the application.src/RegisterAndLoginForm.jsx: Component for user registration and login.src/Chat.jsx: Main chat component that handles real-time messaging.
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature-name
- Make your changes and commit them:
git commit -m "Add your message here"
- Push to the branch:
git push origin feature/your-feature-name
- Open a pull request.
License
This project is licensed under the MIT License.