Real-time chat with group room and 1-to-1 DMs, built using FastAPI + WebSockets, Redis pub/sub (for fan-out & presence), and PostgreSQL (for message history). Frontend is plain HTML/CSS/JS with Jinja templates. Dockerized for easy local runs; CI/CD-friendly for cloud deploys.
Live demo: https://chat-with-randoms.onrender.com/
- πͺ Registration (18+), globally unique username (inline βname takenβ warning)
- π₯ Online presence synced via Redis (works across instances)
- π¬ General room + Direct Messages (DMs)
- π Message history persisted in Postgres
- π Unread badges (red counters) for rooms/DMs
- β»οΈ Local cache per conversation (so messages persist when switching tabs)
- π³ Dockerized; one command local up via Compose
- π Auto-deploy ready (link repo to host = redeploy on every push)
FastAPI (WebSockets) ββ Manages WS connections per instance ββ Publishes to Redis channels (messages & presence) ββ Subscribes to Redis to fan-out to local sockets
Redis (Upstash in prod) ββ Channels: chat_messages:* , user_status_channel ββ Keys: online_users_set (presence), user_profiles (nameβprofile)
PostgreSQL (Render in prod) ββ Stores message history
. ββ backend/ β ββ main.py # FastAPI app (WS, Redis pub/sub, DB) β ββ Dockerfile # Container image for backend β ββ requirements.txt β ββ static/ # Frontend (templates + CSS) β ββ index.html # registration page β ββ chat.html # chat UI + client logic β ββ style.css ββ docker-compose.yml # local dev (backend + redis + postgres) ββ init_db.sql # schema for messages table & index
- Docker Desktop (or Docker + Docker Compose)
- Ports 8000, 6379, 5432 available
# 1) clone
git clone <your-repo-url>
cd <your-repo>
# 2) start services
docker compose up --build.
π Endpoints GET / β registration page POST /register β saves cookie, redirects to /chat GET /chat β chat UI GET /messages/{room_id}?limit=50 β recent messages (room or DM roomId) GET /online_users β currently online users GET /api/name_available?name=... β username availability WS /ws/{user_id} β WebSocket channel
π Credits FastAPI Uvicorn Redis(Upstash in production) PostgreSQL Docker