Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Live Chat

A real-time private messaging app built with FastAPI, WebSockets, PostgreSQL, and Redis. Users authenticate via Google OAuth and can send direct messages to each other with full chat history.

Tech Stack

  • FastAPI — async HTTP + WebSocket server
  • PostgreSQL (via SQLAlchemy + asyncpg) — persistent message and user storage
  • Redis — session storage + chat history cache (cache-aside pattern)
  • Google OAuth 2.0 (via Authlib) — user authentication
  • uv — dependency management

Architecture

Browser  ──HTTP──►  FastAPI
         ◄──WS───►  /ws  (real-time messaging)
                     │
              ┌──────┴──────┐
           Postgres       Redis
          (users/msgs)  (sessions/cache)
  • Sessions are stored in Redis with a 24-hour TTL, keyed by a random token in an httponly cookie.
  • Chat history is cached in Redis for 10 minutes (cache-aside). Sending a new message invalidates the cache for that conversation.
  • WebSocket connections are tracked in-memory per server instance.

Project Structure

app/
├── main.py        # FastAPI app, HTTP routes
├── websocket.py   # WebSocket endpoint
├── models.py      # SQLAlchemy models + DB lifespan
├── auth.py        # OAuth setup, session helpers
├── connect.py     # WebSocket connection manager
└── config.py      # Pydantic settings
static/
└── index.html     # Frontend

Setup

1. Start infrastructure

docker compose up -d

2. Configure environment

cp .env.example .env

Fill in your .env:

DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/chatdb
REDIS_URL=redis://localhost:6379
GOOGLE_CLIENT_ID=<your-google-client-id>
GOOGLE_CLIENT_SECRET=<your-google-client-secret>
SESSION_SECRET=<long-random-string>
ENV=development

To get Google OAuth credentials, create a project at console.cloud.google.com, enable the Google Identity API, and add http://localhost:8000/api/auth/callback as an authorized redirect URI.

3. Run the server

uv run uvicorn app.main:app --reload

Open http://localhost:8000.

API

Method Path Description
GET /api/auth/login Redirect to Google OAuth
GET /api/auth/callback OAuth callback, sets session cookie
POST /api/auth/logout Clear session
GET /api/me Current user profile
GET /api/users All other users
GET /api/chat/history/{peer_email} Chat history with a user
WS /ws WebSocket for real-time messaging

WebSocket message format

Send:

{ "to": "peer@example.com", "text": "Hello!" }

Receive:

{ "sender": "me@example.com", "receiver": "peer@example.com", "text": "Hello!", "timestamp": 1718000000000 }

About

WebSocket Chat with Redis & PostgreSQL

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages