A secure, real-time chat application built with FastAPI, Socket.IO, WebRTC, Redis, and PostgreSQL.
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.
- 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 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 (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
- Image messages
- File messages
- Encrypted file transfers
- Reply to messages containing files/images
- Audio calling
- Video calling
- WebRTC peer-to-peer connections
- Socket.IO-based WebRTC signaling
- Upgrade audio calls to video
- Call history
- Call session management
- Contact list
- Contact search
- Latest message preview
- Unread message counts
- Block contacts
- Unblock contacts
| 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 |
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
Before running the application, make sure you have:
- Python 3.11+
- PostgreSQL
- Redis
- SMTP credentials for password reset emails
git clone https://github.com/MayurMathavadiya/chat-app.git
cd chat-apppython -m venv .venvActivate it:
Linux / macOS
source .venv/bin/activateWindows
.venv\Scripts\activatepip install -r requirements.txtCreate 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.comImportant: Never commit your
.envfile, passwords, API keys, SMTP credentials, or other secrets to GitHub.
Make sure PostgreSQL is running and your database exists.
Then run the Alembic migrations:
alembic upgrade headMake sure Redis is running:
redis-serverOr, if Redis is already installed as a system service:
sudo systemctl start redisStart the FastAPI development server:
uvicorn app.main:app --reloadThe application will be available at:
http://127.0.0.1:8000
| 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 |
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
GET /api/contacts
POST /api/contacts/block
POST /api/contacts/unblock
GET /api/messages/{contact_id}
POST /api/messages/clear/{contact_id}
GET /api/calls/history
POST /api/calls
PATCH /api/calls/{call_id}
MK Chats uses Socket.IO for real-time communication between clients and the server.
| 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
Audio and video calls use WebRTC for peer-to-peer media communication.
Socket.IO is used for signaling.
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 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=10080MK 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.
Run the test suite with:
pytestFor more detailed output:
pytest -vAt 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
- 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.
Before deploying MK Chats to production:
- Use a strong randomly generated
SECRET_KEY. - Never expose
.envor 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.
Add your preferred open-source license to the repository, such as MIT, before publishing the project for reuse.
Mayur Mathavadiya
GitHub: https://github.com/MayurMathavadiya
Built with β€οΈ using FastAPI, Python, Socket.IO, WebRTC, Redis and PostgreSQL.