Skip to content

Labu1011/realtime-chat-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Realtime Chat Backend

Backend API for a real-time chat application built with Node.js, Express, MongoDB, and Socket.IO. It handles authentication, user profiles, private messaging, image uploads, and online user presence.

Features

  • JWT-based authentication with httpOnly cookies
  • User signup, login, logout, and auth verification
  • Profile picture upload with Multer + Cloudinary
  • Private 1:1 messaging between users
  • Message image uploads with Multer + Cloudinary
  • Real-time delivery and online presence using Socket.IO
  • MongoDB persistence with Mongoose models

Tech Stack

  • Node.js
  • Express.js
  • MongoDB
  • Mongoose
  • Socket.IO
  • Cloudinary
  • Multer
  • bcryptjs
  • JSON Web Token (JWT)
  • cookie-parser
  • cors
  • dotenv
  • nodemon

Project Structure

backend/
├── src/
│   ├── controllers/
│   ├── lib/
│   ├── middleware/
│   ├── models/
│   ├── routes/
│   ├── seeds/
│   └── index.js
├── package.json
└── README.md

Setup

  1. Install dependencies
cd backend
npm install
  1. Create a .env file in the backend root
PORT=5001
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
CLIENT_URL=http://localhost:5173

CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
  1. Start the development server
npm run dev

API Endpoints

Auth

  • POST /api/auth/signup - Register a new user
  • POST /api/auth/login - Log in and receive auth cookie
  • POST /api/auth/logout - Clear auth cookie
  • GET /api/auth/check - Get the currently authenticated user
  • PUT /api/auth/update-profile - Update profile picture using multipart/form-data

Messages

  • GET /api/messages/users - Get all users except the logged-in user
  • GET /api/messages/:id - Get conversation messages with a specific user
  • POST /api/messages/send/:id - Send a message to a user using JSON text or multipart/form-data with an image

Request Details

POST /api/auth/signup

Body:

{
  "fullName": "John Doe",
  "email": "john@example.com",
  "password": "password123"
}

POST /api/auth/login

Body:

{
  "email": "john@example.com",
  "password": "password123"
}

PUT /api/auth/update-profile

  • Content-Type: multipart/form-data
  • Field name: profilePic

POST /api/messages/send/:id

  • Content-Type: multipart/form-data
  • Text field: text
  • Image field: image

Realtime Events

Socket.IO is used for live chat updates and online presence.

Client to Server

  • Connection query includes userId

Server to Client

  • getOnlineUsers - Sends the list of currently connected user IDs
  • newMessage - Broadcasts a newly sent message to the receiver

Middleware

  • protectRoute - Verifies JWT cookie and protects private routes
  • upload.middleware.js - Handles image uploads with Multer memory storage
  • error.middleware.js - Central error handler for upload and server errors

Data Models

User

  • email
  • fullName
  • password
  • profilePic
  • timestamps

Message

  • senderId
  • receiverId
  • text
  • image
  • timestamps

Notes

  • Authenticated routes require cookies enabled on the client.
  • Images are uploaded to Cloudinary; the database stores the returned secure URL.
  • The server currently allows image uploads up to 5 MB through Multer.
  • Make sure the frontend uses the same field names: profilePic for profile updates and image for chat attachments.

Scripts

  • npm run dev - Start the backend in development mode with nodemon

About

This is a practice project for improving my project building skill

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors