Skip to content

πŸ” FastAPI backend for a real-time, end-to-end encrypted chat app using JWT, WebSockets, and RSA encryption.

Notifications You must be signed in to change notification settings

jishnu70/fluent-fastapi-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

44 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”§ Fluent Backend – FastAPI Server for Secure Chat

This is the backend server for the Fluent Secure Chat App, built using FastAPI. It powers real-time, end-to-end encrypted messaging over WebSockets, handles JWT-based authentication, and manages user/message data with a clean, scalable architecture.


πŸš€ Features

  • πŸ” End-to-End Encryption Support (RSA Key Exchange using Android Keystore)
  • πŸ”‘ JWT Authentication (Access + Refresh Tokens)
  • πŸ” Token Refresh Mechanism
  • πŸ§‘β€πŸ€β€πŸ§‘ User Registration, Login, and Search
  • πŸ“© Message Storage and Retrieval
  • 🌐 Real-Time Messaging via WebSockets
  • πŸ“¦ Modular FastAPI Router Setup
  • πŸ” CORS and Secure Headers Configured
  • πŸ“ SQLite / PostgreSQL support

πŸ“ Project Structure

.
β”œβ”€β”€ main.py                   # FastAPI application entry point
β”œβ”€β”€ database.py               # Database setup and connection
β”œβ”€β”€ requirements.txt          # Python dependencies

β”œβ”€β”€ alembic/                  # DB migrations folder
β”‚   β”œβ”€β”€ env.py
β”‚   β”œβ”€β”€ script.py.mako
β”‚   β”œβ”€β”€ versions/
β”‚   └── README

β”œβ”€β”€ core/                     # Core logic (encryption, chat hub, auth utils)
β”‚   β”œβ”€β”€ authentication.py
β”‚   β”œβ”€β”€ chatHub.py
β”‚   β”œβ”€β”€ encryption.py
β”‚   └── __init__.py

β”œβ”€β”€ crud/                     # Database interaction layer
β”‚   β”œβ”€β”€ MessageCrud.py
β”‚   └── __init__.py

β”œβ”€β”€ models/                   # SQLAlchemy ORM models
β”‚   β”œβ”€β”€ Attachment.py
β”‚   β”œβ”€β”€ Message.py
β”‚   β”œβ”€β”€ User.py
β”‚   └── __init__.py

β”œβ”€β”€ schemas/                  # Pydantic request/response models
β”‚   β”œβ”€β”€ AttachmentSchema.py
β”‚   β”œβ”€β”€ MessageSchema.py
β”‚   β”œβ”€β”€ TokenSchema.py
β”‚   β”œβ”€β”€ UserSchema.py
β”‚   β”œβ”€β”€ PartnerSchema.py
β”‚   └── __init__.py

β”œβ”€β”€ routes/                   # FastAPI route handlers
β”‚   β”œβ”€β”€ authRoutes.py
β”‚   β”œβ”€β”€ messageRoutes.py
β”‚   └── __init__.py

β”œβ”€β”€ websocket/                # (Currently empty, reserved for WebSocket logic)
└── venv/                     # Python virtual environment (excluded from version control)


πŸ” Authentication Flow

  1. πŸ” Register/Login β†’ Get Access + Refresh Tokens
  2. πŸ”„ Use Access Token for protected endpoints
  3. ♻️ Use /refresh to get new tokens when expired
  4. πŸ’¬ Authenticate WebSocket with token: /chat/ws?token=...

πŸ“‘ WebSocket Messaging

  • Clients connect using token auth (?token=...)
  • Messages are encrypted on the client before sending
  • Server stores encrypted message (no decryption happens server-side)
  • Messages are relayed in real-time to the receiver if online

πŸ› οΈ Setup Instructions

1. Clone the Repo

git clone https://github.com/jishnu70/fluent-fastapi-backend.git
cd fluent-fastapi-backend

2. Create Virtual Environment & Install

python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install -r requirements.txt

3. Run the Server

uvicorn main:app --reload

Server will run at http://127.0.0.1:8000


πŸ“¬ Endpoints Overview

Method Endpoint Description
POST /auth/register Register new user
POST /auth/login Login and get JWT tokens
POST /auth/refresh Refresh expired tokens
GET /users/search?query= Search for users
GET /chat/all_messages Get all messages with partner
WS /chat/ws?token= WebSocket for real-time chat

πŸ” Tech Stack

  • FastAPI (Python)
  • SQLAlchemy (ORM)
  • JWT (Authentication)
  • PostgreSQL (Database)
  • WebSockets (Real-time messaging)

πŸ“¦ Future Improvements

  • β›” Expire old messages (auto-delete after X days)
  • πŸ–ΌοΈ Media support (images, audio)
  • πŸ“² Push notification integration (FCM)
  • πŸ“ Switch to PostgreSQL for production
  • πŸ›‘οΈ Rate-limiting and IP-based blocking

πŸ§ͺ Testing with WebSocket

Use Postman or websocat:

websocat "ws://localhost:8000/chat/ws?token=YOUR_ACCESS_TOKEN"

About

πŸ” FastAPI backend for a real-time, end-to-end encrypted chat app using JWT, WebSockets, and RSA encryption.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published