Skip to content

Latest commit

Β 

History

76 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MK Chats

MK Chats Logo

A secure, real-time chat application built with FastAPI, Socket.IO, WebRTC, Redis, and PostgreSQL.

GitHub


About

MK Chats is a modern real-time chat application built with FastAPI.

It provides secure authentication, real-time messaging, End-to-End Encryption (E2EE), file and image sharing, presence indicators, typing indicators, message replies, read receipts, contact management, blocking, and audio/video calling using WebRTC.

The application uses Socket.IO for real-time communication and Redis for presence and message/event coordination.


✨ Features

πŸ” Authentication & Security

  • User registration and login
  • Cookie-based authentication
  • Bearer token authentication support
  • Password change
  • Email-based password reset
  • Content Security Policy (CSP) middleware
  • Privacy Policy and Terms & Conditions pages

πŸ’¬ Real-Time Messaging

  • Real-time one-to-one messaging
  • Socket.IO-powered communication
  • Typing indicators
  • Online/offline presence
  • Read receipts
  • Message editing
  • Message deletion
  • Message replies
  • Unread message counts
  • Message history
  • Clear chat functionality
  • Background cleanup of cleared messages

πŸ”’ End-to-End Encryption

  • End-to-End Encryption (E2EE) for text messages
  • Encrypted file and image transfers
  • Client-side cryptographic key handling
  • DEK/KEK-based encryption architecture
  • Encryption keys stored securely on the server

πŸ“Ž Files & Media

  • Image messages
  • File messages
  • Encrypted file transfers
  • Reply to messages containing files/images

πŸ“ž Audio & Video Calls

  • Audio calling
  • Video calling
  • WebRTC peer-to-peer connections
  • Socket.IO-based WebRTC signaling
  • Upgrade audio calls to video
  • Call history
  • Call session management

πŸ‘₯ Contacts

  • Contact list
  • Contact search
  • Latest message preview
  • Unread message counts
  • Block contacts
  • Unblock contacts

πŸ› οΈ Tech Stack

Technology Purpose
Python Programming language
FastAPI Backend web framework
SQLAlchemy ORM / database interaction
Alembic Database migrations
PostgreSQL Primary database
Redis Presence and real-time coordination
Socket.IO Real-time communication
Jinja2 Server-side HTML rendering
WebRTC Peer-to-peer audio/video calls
Pytest Automated testing

πŸ“ Project Structure

chat-app/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ routers.py             # Root API router
β”‚   β”œβ”€β”€ api_user.py            # Authentication and profile logic
β”‚   β”œβ”€β”€ api_contact.py         # Contacts and blocking logic
β”‚   β”œβ”€β”€ api_message.py         # Messaging and chat logic
β”‚   β”œβ”€β”€ api_call.py            # Call history logic
β”‚   β”œβ”€β”€ main.py                # FastAPI application setup
β”‚   β”œβ”€β”€ models.py              # SQLAlchemy models
β”‚   β”œβ”€β”€ schemas.py             # Pydantic schemas
β”‚   β”œβ”€β”€ web_page.py            # Web page routes
β”‚   β”œβ”€β”€ socket_events.py       # Socket.IO event handlers
β”‚   β”‚
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   └── ...                # Database, authentication and core utilities
β”‚   β”‚
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── ...                # Background tasks and services
β”‚   β”‚
β”‚   β”œβ”€β”€ static/
β”‚   β”‚   └── ...                # CSS, JavaScript, images and branding
β”‚   β”‚
β”‚   └── templates/
β”‚       └── ...                # Jinja2 templates
β”‚
β”œβ”€β”€ alembic/
β”‚   └── ...                    # Database migrations
β”‚
β”œβ”€β”€ test/
β”‚   └── ...                    # Automated tests
β”‚
β”œβ”€β”€ requirements.txt
└── README.md

πŸ“‹ Prerequisites

Before running the application, make sure you have:

  • Python 3.11+
  • PostgreSQL
  • Redis
  • SMTP credentials for password reset emails

πŸš€ Installation

1. Clone the repository

git clone https://github.com/MayurMathavadiya/chat-app.git
cd chat-app

2. Create a virtual environment

python -m venv .venv

Activate it:

Linux / macOS

source .venv/bin/activate

Windows

.venv\Scripts\activate

3. Install dependencies

pip install -r requirements.txt

βš™οΈ Environment Variables

Create a .env file in the project root:

SQLALCHEMY_DATABASE_URL=postgresql://username:password@localhost:5432/mk_chats

REDIS_URL=redis://localhost:6379/0

SECRET_KEY=your-secret-key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=10080

SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=your-email@example.com
SMTP_PASSWORD=your-email-password-or-app-password
SMTP_FROM_EMAIL=your-email@example.com

Important: Never commit your .env file, passwords, API keys, SMTP credentials, or other secrets to GitHub.


πŸ—„οΈ Database Setup

Make sure PostgreSQL is running and your database exists.

Then run the Alembic migrations:

alembic upgrade head

πŸ”΄ Start Redis

Make sure Redis is running:

redis-server

Or, if Redis is already installed as a system service:

sudo systemctl start redis

▢️ Run the Application

Start the FastAPI development server:

uvicorn app.main:app --reload

The application will be available at:

http://127.0.0.1:8000

🌐 Web Routes

Route Description
/ Chat application
/login Login page
/register Registration page
/forgot-password Password reset request
/reset-password Password reset
/privacy Privacy Policy
/terms Terms & Conditions

πŸ”Œ REST API

Authentication & Profile

POST   /api/register
POST   /api/login
POST   /api/logout
POST   /api/forgot-password
POST   /api/reset-password

GET    /api/profile
PATCH  /api/profile
POST   /api/profile/password

Contacts

GET    /api/contacts
POST   /api/contacts/block
POST   /api/contacts/unblock

Messages

GET    /api/messages/{contact_id}
POST   /api/messages/clear/{contact_id}

Calls

GET    /api/calls/history
POST   /api/calls
PATCH  /api/calls/{call_id}

⚑ Socket.IO

MK Chats uses Socket.IO for real-time communication between clients and the server.

Messaging Events

Event Description
send Send a message
edit Edit an existing message
delete Delete a message
typing Broadcast typing status
mark_read Mark messages as read
presence Broadcast online/offline status

The send event supports features such as:

reply_to_id
file_data

πŸ“ž WebRTC Signaling

Audio and video calls use WebRTC for peer-to-peer media communication.

Socket.IO is used for signaling.

Signaling Events

webrtc_offer
webrtc_answer
webrtc_ice_candidate

webrtc_upgrade_request
webrtc_upgrade_response

webrtc_end

The actual audio/video media connection is established using WebRTC peer connections.


πŸ” Authentication

Authentication can be performed using an access_token cookie or an HTTP Authorization header.

Example:

Authorization: Bearer <access_token>

The application supports long-lived access tokens using the configured:

ACCESS_TOKEN_EXPIRE_MINUTES=10080

πŸ”’ End-to-End Encryption

MK Chats is designed around an End-to-End Encryption architecture.

Cryptographic keys are derived and handled on the client, while encrypted key material can be stored by the server.

The goal is to prevent the server from directly accessing the plaintext contents of encrypted messages and files.

Security Note: E2EE security depends on the complete client-side cryptographic implementation, key management, authentication, and deployment configuration. Review the implementation carefully before using this project for sensitive or production communication.


πŸ§ͺ Testing

Run the test suite with:

pytest

For more detailed output:

pytest -v

πŸ—οΈ Architecture

At a high level, MK Chats works like this:

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚      Browser     β”‚
                    β”‚                  β”‚
                    β”‚  Jinja2 + JS     β”‚
                    β”‚  WebRTC Client   β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                    HTTP / Socket.IO
                             β”‚
                             β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚     FastAPI      β”‚
                    β”‚                  β”‚
                    β”‚ REST API         β”‚
                    β”‚ Authentication   β”‚
                    β”‚ Socket.IO        β”‚
                    β”‚ WebRTC Signaling β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚             β”‚             β”‚
              β–Ό             β–Ό             β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚PostgreSQLβ”‚  β”‚  Redis   β”‚  β”‚  SMTP    β”‚
        β”‚          β”‚  β”‚          β”‚  β”‚          β”‚
        β”‚  Data    β”‚  β”‚ Presence β”‚  β”‚  Email   β”‚
        β”‚  Storage β”‚  β”‚  Events  β”‚  β”‚  Reset   β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

For audio/video calls:

       User A                         User B
          β”‚                              β”‚
          β”‚        Socket.IO             β”‚
          │◄────── Signaling ───────────►│
          β”‚                              β”‚
          β”‚                              β”‚
          └──────── WebRTC β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                Peer-to-Peer Media

πŸ“Œ Development Notes

  • Socket.IO uses Redis for real-time coordination and presence.
  • WebRTC handles peer-to-peer audio/video communication.
  • Jinja2 is used for server-rendered pages.
  • PostgreSQL stores application data.
  • Alembic manages database schema migrations.
  • Background services handle cleanup-related tasks.
  • CSP middleware provides additional browser-side security.
  • Authentication supports both cookies and Bearer tokens.

⚠️ Production Considerations

Before deploying MK Chats to production:

  • Use a strong randomly generated SECRET_KEY.
  • Never expose .env or credentials.
  • Use HTTPS.
  • Configure secure cookies.
  • Use a production PostgreSQL instance.
  • Use a secured Redis instance.
  • Configure a proper SMTP provider.
  • Review WebRTC/STUN/TURN configuration.
  • Review the E2EE implementation and key-management model.
  • Run the application behind a production ASGI server/reverse proxy.
  • Configure appropriate CORS and CSP policies.

πŸ“„ License

Add your preferred open-source license to the repository, such as MIT, before publishing the project for reuse.


πŸ‘¨β€πŸ’» Author

Mayur Mathavadiya

GitHub: https://github.com/MayurMathavadiya


Built with ❀️ using FastAPI, Python, Socket.IO, WebRTC, Redis and PostgreSQL.


Star History

Star History Chart