Django Realtime Events
Project description
🚀 django-realtime-events
Lightweight realtime updates for Django using WebSocket (Django Channels).
django-realtime-events allows you to automatically send realtime events (create, update, delete) from your Django models to the frontend — with minimal setup.
✨ Features
- ⚡ Realtime model updates (create, update, delete)
- 🧩 Plug & play (just register your model)
- 🎯 Per-model WebSocket channel
- 🔍 Select specific fields (optimized payload)
- 🔥 Custom serializer support (full control)
- 🧠 Safe default (ID-only payload)
- 🔌 Supports Redis & InMemoryChannelLayer
- 🪶 Lightweight and easy to integrate
📦 Installation
pip install django-realtime-events
Add to your INSTALLED_APPS:
INSTALLED_APPS = [
...
"channels",
"django_realtime_events",
]
⚙️ Django Channels Setup
1. ASGI Configuration
asgi.py:
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from django_realtime_events.routing import websocket_urlpatterns
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": URLRouter(websocket_urlpatterns),
})
settings.py:
ASGI_APPLICATION = "your_project.asgi.application"
2. Channel Layer (Development)
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer"
}
}
3. (Optional) Production with Redis
Install:
pip install channels-redis
Configure:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}
🚀 Quick Start
1. Register a Model
from django_channels_helper import register_model
register_model("shop.Product")
2. WebSocket Endpoint
ws://localhost:8000/ws/realtime/product/
3. Frontend Example
const socket = new WebSocket("ws://localhost:8000/ws/realtime/product/");
socket.onmessage = function(event) {
const data = JSON.parse(event.data);
console.log(data);
};
📡 Event Format
{
"model": "Product",
"action": "created",
"data": {
"id": 1
}
}
🎯 Data Configuration
🔹 1. Default (ID only)
register_model("shop.Product")
Output:
{ "id": 1 }
🔹 2. Select Fields
register_model(
"shop.Product",
fields=["id", "name"]
)
Output:
{
"id": 1,
"name": "Laptop"
}
🔹 3. Custom Serializer 🔥
def product_serializer(obj):
return {
"id": obj.id,
"name": obj.name,
"category": obj.category.name
}
register_model(
"shop.Product",
serializer=product_serializer
)
Output:
{
"id": 1,
"name": "Laptop",
"category": "Electronics"
}
⚠️ Data Priority Rule
serializer > fields > default (id only)
- If
serializeris provided → it will be used - Else if
fieldsis provided → selected fields will be used - Else → only
idwill be sent
🧠 Why ID-only by Default?
The default behavior sends only the model ID to ensure:
- 🔐 No accidental data exposure
- ⚡ Minimal payload (fast & efficient)
- 🧩 Works well with event-driven architecture
- 🎯 Full flexibility for developers
🧱 Multiple Models
You can register multiple models:
register_model("shop.Product")
register_model("shop.Order")
WebSocket endpoints:
/ws/realtime/product/
/ws/realtime/order/
🧩 Example Use Cases
- 📊 Live dashboards
- 🛒 Realtime product updates
- 🔔 Notification systems
- 📋 Auto-refresh tables
- 💬 Chat triggers
⚠️ Best Practices
- Use
fieldsfor better performance - Use
serializerfor relations and transformations - Avoid heavy queries inside serializer
- Use Redis in production environments
🛠️ Requirements
- Python 3.12+
- Django 5.1+
- Django Channels
- Daphne 4.2+ (Optional)
⚡ Running the Server
This package uses WebSocket via Django Channels, which requires an ASGI server.
Development
If Django Channels is properly configured, you can use:
python manage.py runserver
Alternative (Recommended for stability)
You can also run the project using Daphne:
daphne your_project.asgi:application
If WebSocket is not working with runserver, try using Daphne.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_realtime_events-1.0.2.tar.gz.
File metadata
- Download URL: django_realtime_events-1.0.2.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ef0607e14dbb13d2bf4d0cc9f76407d57a46da3015ac82f1ee957b79dc45637
|
|
| MD5 |
96292ac454c9fa5269b8f23832a574b0
|
|
| BLAKE2b-256 |
50bf2f827febc2e23f73a4011be99ab531e6cc4d0ef2bc4d085b3181ff634a8f
|
File details
Details for the file django_realtime_events-1.0.2-py3-none-any.whl.
File metadata
- Download URL: django_realtime_events-1.0.2-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87f62e38a3ec3707c96966db43358f78cc9febb4a0677297c60a00388a2b63b1
|
|
| MD5 |
abbc6b04394ebf6aa607d377fa86caf4
|
|
| BLAKE2b-256 |
43c03374ff8fbfbb91ddc6d0b4a258125c22bc17da3cfc475339dd11f051ae48
|