SwiftChat is a modern real-time chat application built with FastAPI, WebSockets, and Neon PostgreSQL.
Features JWT authentication, live messaging, chat room management, and persistent chat history.
- ๐ User Authentication - Secure registration and login with JWT tokens
- ๐ฌ Real-time Messaging - Instant messaging with WebSocket connections
- ๐ Chat Rooms - Create and join different chat rooms
- ๐ฑ Responsive UI - Clean, modern interface that works on all devices
- ๐พ Message Persistence - Chat history stored in PostgreSQL database
- ๐ Single-Service Deployment - Frontend and backend served together
- ๐ Secure - Password hashing with bcrypt and JWT token authentication
| Technology | Purpose | Features |
|---|---|---|
| ๐ FastAPI | Web Framework | High-performance async API, Auto-documentation, Type hints |
| ๐๏ธ SQLAlchemy | Database ORM | Advanced querying, Relationship mapping, Migration support |
| ๐ PostgreSQL (Neon) | Cloud Database | Serverless, Auto-scaling, High availability |
| โก WebSockets | Real-time Communication | Bi-directional, Low latency, Event-driven |
| ๐ JWT | Authentication | Stateless, Secure, Token-based auth |
| ๐ก๏ธ bcrypt | Password Security | Adaptive hashing, Salt generation, Brute-force resistant |
| ๐ง Uvicorn | ASGI Server | High performance, Production ready, Hot reload |
| ๐ฆ Pydantic | Data Validation | Type validation, Serialization, Auto-documentation |
| Technology | Purpose | Features |
|---|---|---|
| ๐ HTML5 | Structure | Semantic markup, Accessibility, Modern standards |
| ๐จ CSS3 | Styling | Flexbox/Grid, Animations, Responsive design |
| โก Vanilla JavaScript | Interactivity | WebSocket client, Async/await, DOM manipulation |
| ๐ฑ Responsive Design | Mobile Support | Adaptive layouts, Touch-friendly, Cross-browser |
| Technology | Purpose | Features |
|---|---|---|
| ๐ Render | Hosting Platform | Free tier, Auto-deploy, HTTPS, Global CDN |
| ๐ Neon | Database Hosting | Serverless PostgreSQL, Branching, Auto-scaling |
| ๐ Git | Version Control | Distributed, Branching, Collaboration |
| ๐ GitHub | Code Repository | CI/CD integration, Issue tracking, Pull requests |
| Feature | Implementation | Benefit |
|---|---|---|
| ๐ CORS Protection | FastAPI Middleware | Cross-origin security |
| ๐ก๏ธ SQL Injection Prevention | SQLAlchemy ORM | Parameterized queries |
| ๐ Environment Variables | python-dotenv | Secure configuration |
| โก Async Architecture | FastAPI + Uvicorn | High concurrency |
| ๐ Static File Serving | FastAPI StaticFiles | Optimized delivery |
graph TB
A[๐ค User Browser] --> B[๐ Frontend HTML/CSS/JS]
B --> C[โก WebSocket Connection]
B --> D[๐ REST API Calls]
C --> E[๐ฅ๏ธ FastAPI Backend]
D --> E
E --> F[๐๏ธ SQLAlchemy ORM]
F --> G[๐ PostgreSQL Database]
E --> H[๐ JWT Authentication]
E --> I[๐ก๏ธ Security Middleware]
SwiftChat/
โโโ app/
โ โโโ __init__.py
โ โโโ main.py # FastAPI app with static serving
โ โโโ config.py # Environment configuration
โ โโโ database.py # Database connection
โ โโโ models.py # SQLAlchemy models
โ โโโ schemas.py # Pydantic schemas
โ โโโ websocket.py # WebSocket handling
โ โโโ routes/
โ โโโ auth.py # Authentication endpoints
โ โโโ chat.py # Chat endpoints
โโโ frontend/
โ โโโ login.html # Login page
โ โโโ register.html # Registration page
โ โโโ chat.html # Chat interface
โ โโโ styles.css # Shared styles
โโโ requirements.txt # Python dependencies
โโโ .env # Environment variables
โโโ README.md
- Python 3.8+
- PostgreSQL database (or Neon account)
-
Clone the repository
git clone <your-repo-url> cd SwiftChat
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables
cp .env.example .env # Edit .env with your database credentials -
Run the application
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
-
Access the application
- Open your browser to
http://localhost:8000 - Register a new account or login
- Start chatting!
- Open your browser to
-
Push to GitHub
git add . git commit -m "Ready for deployment" git push origin main
-
Create Render Web Service
- Connect your GitHub repository
- Build Command:
pip install -r requirements.txt - Start Command:
uvicorn app.main:app --host 0.0.0.0 --port $PORT
-
Environment Variables
DATABASE_URL=postgresql://your-neon-db-url JWT_SECRET=your-super-secret-jwt-key ENVIRONMENT=production DEBUG=False -
Access your deployed app
- Frontend:
https://your-app.onrender.com/ - API Docs:
https://your-app.onrender.com/docs
- Frontend:
POST /auth/register- Register new userPOST /auth/login- User login
GET /chat/messages/{room_id}- Get chat historyPOST /chat/messages/- Send messageWS /ws/chat/{room_id}- WebSocket connection
GET /health- Health checkGET /docs- API documentation
- Register a new account with username, email, and password
- Login with your credentials
- Enter a room ID (any number) to join a chat room
- Start messaging - messages are delivered in real-time
- View history - previous messages are loaded automatically
- Password Hashing with bcrypt
- JWT Token Authentication
- CORS Protection
- SQL Injection Prevention with SQLAlchemy ORM
- Environment Variable Security
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- FastAPI for the excellent web framework
- Neon for managed PostgreSQL hosting
- Render for easy deployment platform
Built with โค๏ธ by [Sudipta Sarkar]
Live Demo: SwiftChat on Render