Skip to main content

Django Channels Router (Backend, PyPI)

A lightweight Django + Channels router that lets your WebSocket consumers behave like HTTP endpoints.
Designed to pair seamlessly with @djanext/observable-socket.


Features

  • 🧭 Route names → handler methods (sayHello → on_say_hello)
  • ⚡ Both sync & async consumers
  • 🧩 Optional hydrate / dehydrate functions
  • 🔁 Built-in heartbeat support (PING/PONG)
  • 📦 Typed results and HTTP-style status codes

Requirements

  • Python ≥ 3.11
  • Django ≥ 4.2
  • Channels ≥ 4.0
  • Redis (or any supported channel layer)

Installation

pip install django-channels-router

In your project:

# routing.py
from django.urls import re_path
from django_channels_router import SocketRouterConsumer

websocket_urlpatterns = [
    re_path(r"ws/app/$", SocketRouterConsumer.as_asgi()),
]

Example Consumer

from django_channels_router import SocketRouterConsumer, StatusCodes

class AppSocket(SocketRouterConsumer):
    @SocketRouterConsumer.routes.setter
    def routes(self, _):
        self._routes = [
            {"route": "sayHello"},
            {"route": "getArticle",
             "hydrate": lambda headers, p: load_article(p["id"]),
             "dehydrate": lambda art: {"id": art.id, "title": art.title}
             },
        ]

    def on_say_hello(self, payload, headers):
        name = (payload or {}).get("name", "World")
        return {"status": StatusCodes.OK, "payload": f"Hello, {name}!"}

Async Example

from django_channels_router import AsyncSocketRouterConsumer

class AppSocketAsync(AsyncSocketRouterConsumer):
    @AsyncSocketRouterConsumer.routes.setter
    def routes(self, _):
        self._routes = [{"route": "sayHello"}]

    async def on_say_hello(self, payload, headers):
        return {"status": 200, "payload": {"msg": "Hello async!"}}

Serializing and Deserializing of messages

hydrate and dehydrate functions helps to focus only on logic part as these functions will handle deserializing and serializing automatically. To use them, first you need to implement the converters (loader, serializer, ...)

def load_article(headers, payload) -> Article | None:
    try:
        article_id = headers.get('id')
        if not article_id:
            return None
        return Article.objects.get(id=article_id) # assume id is of type string like uuid
    except Article.DoesNotExist:
        return None

def deserialize(headers, payload) -> Article | None:
    try:
        serializer = ArticleSerializer(data=payload)
        serializer.is_valid(raise_exception=True)
        return serializer.save()
    except serializers.ValidationError:
        return None

def serialize(article: Article) -> str:
    serializer = ArticleSerializer(article)
    return serializer.data

now these converters can be used as follows:

    class ArticleConsumer(SocketRouterConsumer):
        routes = [
            {route: get, hydrate: load_article, dehydrate: serialize},
            {route: create, hydrate: deserialize, dehydrate: serialize}
        ]
        
        def on_get(headers, payload):
            return {
                payload: payload, 
                # article gets fetched from DB in background using the `load_article` function
                # dehydrate will automatically serilize the article by running `serialize` function
                status: StatusCodes.OK if payload else StatusCodes.NOT_FOUND 
            }
            
        def on_create(headers, payload):
            if not payload: # deserialize function has returned None
                return {
                    payload: "Unable to create article",
                    status: StatusCodes.BAD_REQUEST
                }
                
            return {
                payload: payload,
                status: StatusCodes.CREATED
            }

Note:

hydrate function only applies if returned status code by user is 200 series. (199 < status code < 300)


Status Codes

Symbol Value Meaning
StatusCodes.OK 200 Success
StatusCodes.BAD_REQUEST 400 Malformed message
StatusCodes.NOT_FOUND 404 Unknown route
StatusCodes.INTERNAL_SERVER_ERROR 500 Handler failure

Good Practices

  • Always accept() connections in connect() / await accept().
  • Access result.get("payload") safely — avoid missing keys.
  • Use @classmethod route definitions if you plan to reuse the router base.
  • Keep payload JSON-serializable.

Frontend Client

Pair with @djanext/observable-socket
→ Handles auto-reconnect, multiple sockets, and sendAndWait() with Promises.


License

MIT

Metadata

Release files for django-channels-router 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-channels-router 1.0.3
File Size Uploaded Provenance
django_channels_router-1.0.3.tar.gz 7.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-channels-router 1.0.3
File Interpreter ABI Platform
django_channels_router-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 20.1 kB

Release files / django_channels_router-1.0.3.tar.gz

Download URL django_channels_router-1.0.3.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
23d7b456260480c231ebb03993cb60f0a92a6a87ace5cf5f9903474584cdfa3f
BLAKE2b-256 checksum
How to use checksums
274d43c69dff6f6fd2a3137b94f4a83f32a5cb79bced4f4ffe43c933f201836f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / django_channels_router-1.0.3-py3-none-any.whl

Download URL django_channels_router-1.0.3-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
25f1a4655c1ddbd265d08dbbefdd305f9113b7010a7c9c2a542c6c117159f358
BLAKE2b-256 checksum
How to use checksums
f682df0d9af3fa3196d467a22fc4b2f81d2765b33402c38e6e66aeb8ef30337c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Security for django-channels-router 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page