A real-time chat application built with React, Node.js, Express, and Socket.IO. This application provides a responsive and user-friendly interface for exchanging messages in real time, with secure user authentication and persistent chat history.
- Backend Repository: Chat App Backend
- Real-time messaging powered by Socket.IO.
- Secure user authentication using JWT.
- Responsive UI with dynamic chat areas and contact lists.
- Persistent chat history fetched from the backend.
- Message timestamps and UI differentiation for sent and received messages.
- React: Modular and efficient UI library.
- Axios: For API requests.
- CSS (or styled-components): For styling and responsive design.
- Node.js: Server-side runtime environment.
- Express.js: Lightweight API framework.
- MongoDB: NoSQL database for structured and unstructured data.
- JWT: For secure authentication.
- Socket.IO: For real-time communication.
- Node.js: Version 16 or later.
- npm or yarn: For package management.
- MongoDB: A local or cloud-based instance.
-
Clone the repositories:
- Frontend:
git clone https://github.com/prajit96/chat-app-frontend.git
- Backend:
git clone https://github.com/prajit96/chat-app-backend.git
- Frontend:
-
Install dependencies:
- Frontend:
cd chat-app-frontend npm install
- Backend:
cd chat-app-backend npm install
- Frontend:
-
Set up environment variables:
- Backend: Create a
.env
file in the root directory with the following variables:MONGO_URI=your-mongo-db-uri JWT_SECRET=your-jwt-secret SOCKET_PORT=your-port
- Backend: Create a
-
Start the servers:
- Frontend:
npm start
- Backend:
npm run dev
- Frontend:
-
Open the application in your browser at
http://localhost:3000
.
Frontend: Chat App Frontend
-
Register User
- POST
/api/auth/register
- Request Body:
{ "username": "string", "email": "string", "password": "string" }
- Response:
{ "message": "User registered successfully", "token": "jwt-token" }
- POST
-
Login User
- POST
/api/auth/login
- Request Body:
{ "email": "string", "password": "string" }
- Response:
{ "message": "Login successful", "token": "jwt-token" }
- POST
- Get Contacts
- POST
/api/contacts
- Headers:
{ "Authorization": "Bearer <token>" }
- Response:
{ "contacts": [ { "_id": "string", "username": "string" } ] }
- POST
-
Get Messages
- GET
/api/chats/:contactId
- Headers:
{ "Authorization": "Bearer <token>" }
- Response:
{ "messages": [ { "sender": "string", "text": "string", "timestamp": "date" } ] }
- GET
-
Send Message
- POST
/api/chats/:contactId/messages
- Headers:
{ "Authorization": "Bearer <token>" }
- Request Body:
{ "text": "string" }
- Response:
{ "message": { "sender": "string", "text": "string", "timestamp": "date" } }
- POST
- React: Chosen for its modularity, component reusability, and ease of state management.
- Socket.IO: Provides seamless real-time communication between the client and server.
- Dynamic Styling: Ensures clear differentiation between sent and received messages for better user experience.
- Responsive Design: Tailored for a variety of screen sizes, ensuring usability across devices.
- Node.js & Express: Lightweight, scalable frameworks for API development.
- MongoDB: Efficiently handles both structured and unstructured data for storing users, messages, and contacts.
- JWT: Implements secure authentication and token-based session management.
- Socket.IO on Backend: Facilitates real-time updates and synchronization between users.
- Issue: Synchronizing message updates between multiple users.
- Solution: Careful integration of Socket.IO for event-driven, real-time messaging.
- Issue: Securely managing user sessions with token-based authentication.
- Solution: Implemented JWT for authorization and added token verification middleware.
- Issue: Differentiating sent and received messages while maintaining a responsive layout.
- Solution: Used conditional styling with dynamic CSS classes to visually separate messages.
- Issue: Providing clear error messages and handling failed API requests.
- Solution: Standardized error responses on the backend and implemented graceful error handling on the frontend.
- Issue: Ensuring a smooth deployment process for both backend and frontend.
- Solution: Deployed backend on Render and frontend on Vercel, ensuring proper environment configurations.