Skip to content

joykhatri/Chat-App-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 

Repository files navigation

Chat-App-Backend

The Chat Application is a real-time messaging system that allows users to communicate instantly through text messages using WebSockets.

🚀 Setup Instructions

1️⃣ Create Virtual Environment

python -m venv .venv

2️⃣ Activate Virtual Environment

.venv\Scripts\activate

Linux/macOS:

source .venv/bin/activate

3️⃣ Install Dependencies

pip install django djangorestframework mysqlclient
pip install django-filter
pip install djangorestframework-simplejwt     # JWT Authentication
pip install channels                          # WebSocket support
pip install daphne                            # ASGI server for WebSocket

4️⃣ Start Django Project & App

django-admin startproject chatproject .
django-admin startapp chatapp

5️⃣ Add Apps to INSTALLED_APPS (project/settings.py)

INSTALLED_APPS = [
    ...
    'chatapp',
    'rest_framework',
    'rest_framework_simplejwt.token_blacklist',
    'django_filters',
    'channels',
]

6️⃣ Configure MySQL Database (settings.py)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or your DB host
        'PORT': '3306',
    }
}

7️⃣ Apply Migrations

python manage.py makemigrations
python manage.py migrate

8️⃣ Run Server

Development server:

python manage.py runserver

ASGI/Daphne server (for WebSocket):

$env:DJANGO_SETTINGS_MODULE="chatproject.settings"   # Windows PowerShell
daphne -p 8000 chatproject.asgi:application

🔑 API Endpoints

Method Endpoint Description
POST /api/register/ User Registration
POST /api/login/ User Login
POST /api/logout/ User Logout
GET /api/profile/ Get Profile info
GET /api/users/ Get User List
GET /api/users/{id}/ Get User Profile info with Id
GET /api/users/?name=demo Search Users
GET /api/users/online/ Get Online Users Info
POST /api/chat/start/ Start new Chat
GET /api/chat/{chat_id}/messages/ All Messages
DELETE /api/chat/{chat_id}/ Delete Chat
POST /api/group/create/ Create Group
POST /api/group/create/{group_id}/add-member/ Add Members in Group
POST /api/group/create/{group_id}/remove-member/ Add Members from Group
GET /api/group/create/{group_id}/messages/ Fetch Group Messages
DELETE /api/group/create/{group_id}/ Delete Group

Register

{
    "name": "demo",
    "email": "demo@gmail.com",
    "password": "demo@123",
    "is_online": true
}

Login

{
    "email": "demo@gmail.com",
    "password": "demo@123"
}
  • Returns access and refresh tokens
Use access token in Authorization header for protected endpoints:
Authorization: Bearer <access_token>

Logout

{
    "refresh_token": "Your Refresh Token"
}

- Use access token in Authorization header for protected endpoints:
Authorization: Bearer <access_token>
- Use refresh token in payload for Logout

Start Chat

{
    "type": "personal"
}

Create Group

{
  "name": "Demo Group"
}

Add/Remove Member

{
  "user_id": 1
}

🌐 WebSocket Endpoints

For Chat

ws://127.0.0.1:8000/ws/chat/{chat_id}/?user_id={id}

- Message
{
    "event": "send_message",
    "message": "Hello",
    "type": "text"
}

Show all Chat

ws://127.0.0.1:8000/ws/homescreen/?user_id={id}
  • Shows user all chat (Home Screen)
  • Shows who were added to the Group & Removed from the Group

About

The Chat Application is a real-time messaging system that allows users to communicate instantly through text messages using WebSockets.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages