Skip to main content

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 serializer is provided → it will be used
  • Else if fields is provided → selected fields will be used
  • Else → only id will 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 fields for better performance
  • Use serializer for relations and transformations
  • Avoid heavy queries inside serializer
  • Use Redis in production environments

🛠️ Requirements

  • Python 3.12+
  • Django 5.1+
  • Django Channels

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_realtime_events-1.0.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_realtime_events-1.0.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file django_realtime_events-1.0.1.tar.gz.

File metadata

  • Download URL: django_realtime_events-1.0.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for django_realtime_events-1.0.1.tar.gz
Algorithm Hash digest
SHA256 cfcf26f0e09251f6e93bacf834cab9366e0dfd50647beeba6116f942982f3427
MD5 d995f14f3949f81b7cc50946822ad03b
BLAKE2b-256 0187311f40fa6d1f6a5b27f1232aebaa38a9e918fb563120fcebc5d326fd42f5

See more details on using hashes here.

File details

Details for the file django_realtime_events-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_realtime_events-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 24132920cf40b16619e289a718660049f02f92068737ef9cc5ff097fb95e54b4
MD5 d8117b4aa983ab1126514692a1f58ebf
BLAKE2b-256 6cbc81ce259fe97898df9dd6523693489ec153b90b58463b6a359d0dd578c5d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page