FastAPI backend for a real-time chat application. It supports JWT authentication, 1-to-1 and group conversations, message history, and WebSocket-based live messaging backed by Redis Pub/Sub for multi-instance broadcasting.
Quick Links
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
- JWT auth with refresh tokens
- 1-to-1 conversations
- Group conversations with roles (
owner,admin,member) - Group member management
- Message history pagination
- WebSocket real-time messaging
- Redis Pub/Sub for cross-instance fan-out
- FastAPI, Pydantic
- SQLAlchemy (async), Alembic
- PostgreSQL, Redis
- JWT (python-jose), Passlib/Argon2
- Docker, Docker Compose, Pytest
app/api- Versioned API routesapp/api/v1/chat_one_to_one.py- 1-to-1 + conversation endpointsapp/api/v1/chat_group.py- Group management endpointsapp/api/v1/chat_ws.py- WebSocket endpointapp/api/v1/user.py- User endpointsapp/api/v1/auth.py- Auth endpointsapp/models- SQLAlchemy modelsapp/schemas- Pydantic schemasapp/services- Business logicapp/core- Config, security, Redis, WebSocketsapp/db- Async database setupalembic- Migrationstests- Automated tests
API base path is composed from .env values:
API_PREFIX=/apiAPI_V1=/v1
Default base URL becomes http://localhost:8000/api/v1.
cp .env.example .envdocker-compose up -d --build- Open
http://localhost:8000/docs
- Create and fill
.env(see.env.example). - Install deps:
uv syncorpip install -r <generated requirements> - Run:
uvicorn app.main:app --reload
- Login uses OAuth2 password flow (
username+passwordform fields). - Refresh tokens are returned in the response body and also set as an HTTP-only cookie (
refresh_token) scoped to/api/v1/auth/refresh. - Protected endpoints require
Authorization: Bearer <access_token>.
Base URL: /api/v1
GET /- Health checkPOST /sent-notification/{email}- Background notification (query:message)
POST /api/v1/auth/register- Register userPOST /api/v1/auth/login- Login (OAuth2 form)POST /api/v1/auth/refresh- Refresh access tokenGET /api/v1/auth/me- Current user (auth required)
POST /api/v1/users/- Create userGET /api/v1/users/- List users (query:skip,limit)GET /api/v1/users/{user_id}- Get userPUT /api/v1/users/{user_id}- Update user (auth required)DELETE /api/v1/users/{user_id}- Delete user (auth required)
GET /api/v1/chat/conversations- List my conversations (auth required)POST /api/v1/chat/conversations- Get or create conversation (auth required)GET /api/v1/chat/conversations/{conversation_id}/messages- List messages (auth required, query:skip,limit)
POST /api/v1/chat/groups- Create group (auth required)GET /api/v1/chat/groups/{conversation_id}- Group detail (auth required)PATCH /api/v1/chat/groups/{conversation_id}- Update group (auth required)GET /api/v1/chat/groups/{conversation_id}/members- List members (auth required)POST /api/v1/chat/groups/{conversation_id}/members- Add member (auth required)DELETE /api/v1/chat/groups/{conversation_id}/members/{user_id}- Remove member (auth required)PATCH /api/v1/chat/groups/{conversation_id}/members/{user_id}/role- Update member role (auth required)
WS /api/v1/chat/ws/conversations/{conversation_id}- Auth via query string
?token=<access_token>or headerAuthorization: Bearer <access_token>
Client to server:
{"type": "message.send", "content": "hello"}Server to client:
{"type": "message.new", "message": {"id": "...", "content": "..."}}This project is intended for learning and development.