This is the backend for a real-time chat application built using Node.js, Express, MongoDB, and Socket.io. The API provides authentication, messaging, and real-time communication features.
- Node.js (Latest LTS version recommended)
- MongoDB (Local or Cloud)
- Postman or REST Client for testing APIs
-
Clone the repository:
git clone https://github.com/Programming-Sai/Chat-Application-Backend.git cd Chat-Application-Backend -
Install dependencies:
npm install
-
Create a
.envfile in the root directory:PORT= MONGO_DB_URL= MONGODB_PASSWORD= MODE=development || production JWT_SECRET= CLOUDINARY_CLOUD_NAME= CLOUDINARY_API_KEY= CLOUDINARY_API_SECRET= FRONTEND_BASE_URL= BACKEND_BASE_URL= -
Start the server:
npm run devThe server should now be running on http://localhost:5000.
./Chat-Application-Backend/*
├─ src/*
| ├─ controllers/*
| | ├─ auth.controllers.js
| | └─ message.controller.js
| ├─ lib/*
| | ├─ cloudinary.js
| | ├─ db.js
| | ├─ socket.js
| | └─ utils.js
| ├─ middleware/*
| | └─ auth.middleware.js
| ├─ models/*
| | ├─ message.model.js
| | └─ user.model.js
| ├─ routes/*
| | ├─ auth.route.js
| | └─ message.route.js
| └─ index.js
├─ tests/*
| ├─ auth.tests.http
| └─ message.tests.http
├─ .env
├─ .env_skeleton
├─ .gitignore
├─ package-lock.json
├─ package.json
└─ README.md
POST /api/auth/signupRequest Body:
{
"fullName": "Test User",
"email": "testuser@example.com",
"password": "Test@123"
}POST /api/auth/signinRequest Body:
{
"email": "testuser@example.com",
"password": "Test@123"
}Response:
- Returns a JWT token. Use this token for authenticated requests.
GET /api/auth/checkHeaders:
Cookie: jwt=your_token_herePOST /api/auth/signoutHeaders:
Cookie: jwt=your_token_hereGET /api/message/usersGET /api/message/{chat_user_id}POST /api/message/send/{chat_user_id}Request Body:
{
"text": "Hello, how are you?"
}POST /api/message/send/{chat_user_id}Request Body:
{
"image": "https://example.com/sample-image.jpg"
}- Event:
connection - Data:
{ userId }(sent viasocket.handshake.query.userId) - Description: When a user connects, they are added to
userSocketMap, and all online users are updated.
- Event:
getAllOnlineUsers - Data:
{ userIds: [...] } - Description: Broadcasts the list of all currently connected users whenever a user connects or disconnects.
- Event:
disconnect - Description: Removes the user from
userSocketMapand updates the online user list.