A scalable real-time notification system featuring WebSocket delivery, priority-based routing, rate limiting, offline processing, and comprehensive observability with Prometheus metrics and structured logging.
- Real-time WebSocket notifications
- Email notifications with fallback
- Priority-based delivery (high, medium, low)
- Rate limiting per priority level
- Connection management with device limits
- Offline notification processing
- Presence tracking
- Prometheus metrics and structured logging
- JWT authentication for WebSocket connections
- Django 6.0 & Django REST Framework
- Django Channels for WebSocket support
- Celery for async task processing
- PostgreSQL for persistent storage
- Redis for channel layer, caching, and presence tracking
- Prometheus for metrics collection
- Email backend for notifications
- Clone the repository:
git clone <repository-url>
cd django-realtime-notifications- Install dependencies using uv:
uv sync- Set up environment variables:
cp .env.example .envEdit .env with your configuration:
- Database credentials
- Redis URL
- Email settings
- JWT secret key
- Run migrations:
uv run python manage.py migrate- Create a superuser:
uv run python manage.py createsuperuser- Start Redis:
docker run -d -p 6379:6379 --name redis-notifications redis:alpine- Start Celery worker:
uv run celery -A django_realtime_notifications worker --loglevel=info- Start Celery beat (for scheduled tasks):
uv run celery -A django_realtime_notifications beat --loglevel=info- Start Django development server:
uv run python manage.py runserverPOST /api/auth/jwt/create/- Get JWT tokenPOST /api/auth/jwt/refresh/- Refresh JWT token
GET /api/notifications/- List notifications (supports filtering)POST /api/notifications/- Create notificationGET /api/notifications/{id}/- Get notification detailsPATCH /api/notifications/{id}/- Update notificationDELETE /api/notifications/{id}/- Delete notificationPATCH /api/notifications/{id}/mark_read/- Mark as readGET /api/notifications/stats/- Get notification statistics
GET /api/metrics/- Prometheus metrics endpoint
Connect to WebSocket endpoint:
ws://localhost:8000/ws/notifications/?token=<JWT_TOKEN>
Incoming:
notification- New notificationmissed_notifications- Notifications received while offlinepong- Heartbeat response
Outgoing:
ping- Heartbeat to maintain connection
Open frontend/index.html in a browser to see the real-time notification demo.
Features:
- Real-time notification display
- Connection status indicator
- Disconnect/reconnect functionality
- Missed notification recovery
- Priority-based styling
- High priority: 100 requests/hour
- Medium priority: 50 requests/hour
- Low priority: 20 requests/hour
- Maximum 5 concurrent connections per user
- Configurable in
notifications/services/connection.py
websocket- Real-time delivery via WebSocketemail- Email notificationboth- Both WebSocket and email
- Failed notifications retry after 60 seconds
- Maximum delivery attempts tracked
- Exponential backoff for retries
Access metrics at http://localhost:8000/api/metrics/
Available metrics:
notifications_created_total- Total notifications creatednotifications_delivered_total- Total notifications deliverednotifications_failed_total- Total failed deliveriesnotification_delivery_latency_seconds- Delivery latency histogramactive_websocket_connections- Current WebSocket connectionspending_notifications_count- Pending notifications
Logs are output in JSON format for easy parsing by log aggregation tools.
- Copy environment file:
cp .env.example .env-
Update
.envwith your configuration (SECRET_KEY, database credentials, etc.) -
Build and start all services:
docker compose up --build- Run migrations:
docker compose exec django uv run python manage.py migrate- Create superuser:
docker compose exec django uv run python manage.py createsuperuserThe application will be available at http://localhost:8000
- django: Django application with Daphne ASGI server (port 8000)
- postgres: PostgreSQL database (port 5432)
- redis: Redis for caching, channel layer, and Celery (port 6379)
- celery-worker: Celery worker for async tasks
- celery-beat: Celery beat scheduler for periodic tasks
docker compose downTo remove volumes as well:
docker compose down -vRun tests:
uv run pytestRun with coverage:
uv run pytest --cov=notifications --cov-report=htmlCheck code style:
uv run ruff check .Format code:
uv run ruff format .- Set
DEBUG=Falsein.env - Generate a secure
SECRET_KEY - Update
ALLOWED_HOSTSwith your domain - Configure proper email backend (SMTP)
- Use PostgreSQL for database
- Set up SSL/TLS certificates
- Configure Prometheus scraping for metrics
- Set up log aggregation (ELK, Loki, CloudWatch)
MIT