Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Programming-Sai/Chat-Application-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

5 Commits

Repository files navigation

Chat-Application-Backend

Overview

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.

Setup

Prerequisites

  • Node.js (Latest LTS version recommended)
  • MongoDB (Local or Cloud)
  • Postman or REST Client for testing APIs

Installation

  1. Clone the repository:

    git clone https://github.com/Programming-Sai/Chat-Application-Backend.git
    cd Chat-Application-Backend
  2. Install dependencies:

    npm install
  3. Create a .env file 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=
  4. Start the server:

npm run dev

The server should now be running on http://localhost:5000.

Project Structure


./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

API Endpoints

Authentication

Signup a new user

POST /api/auth/signup

Request Body:

{
 "fullName": "Test User",
 "email": "testuser@example.com",
 "password": "Test@123"
}

Sign in an existing user

POST /api/auth/signin

Request Body:

{
 "email": "testuser@example.com",
 "password": "Test@123"
}

Response:

  • Returns a JWT token. Use this token for authenticated requests.

Check authenticated user

GET /api/auth/check

Headers:

Cookie: jwt=your_token_here

Logout user

POST /api/auth/signout

Headers:

Cookie: jwt=your_token_here

Messaging

Get all users for sidebar (excluding logged-in user)

GET /api/message/users

Get messages between logged-in user and another user

GET /api/message/{chat_user_id}

Send a new text message

POST /api/message/send/{chat_user_id}

Request Body:

{
 "text": "Hello, how are you?"
}

Send an image message

POST /api/message/send/{chat_user_id}

Request Body:

{
 "image": "https://example.com/sample-image.jpg"
}

WebSocket Events

User Connection

  • Event: connection
  • Data: { userId } (sent via socket.handshake.query.userId)
  • Description: When a user connects, they are added to userSocketMap, and all online users are updated.

Get Online Users

  • Event: getAllOnlineUsers
  • Data: { userIds: [...] }
  • Description: Broadcasts the list of all currently connected users whenever a user connects or disconnects.

User Disconnect

  • Event: disconnect
  • Description: Removes the user from userSocketMap and updates the online user list.

About

A scalable and efficient backend for a realtime chat application built using Node.js, WebSockets, and a database for message storage. This backend will support user authentication, message delivery, and online status tracking.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

AltStyle によって変換されたページ (->オリジナル) /