Skip to main content

django-channels-handlers

Latest PyPI version image

Django Channels consumers, without the Pain 💊

django-channels-handers is an abstraction for Django Channels that makes it easy to implement elegant protocols without having to worry about the communication layer.

Requirements

  • Django>=2.1
  • channels~=2.4
  • pydantic~=1.4

Usage

Install django-channels-handlers from pypi:

pip install django-channels-handlers

Create pydantic models for each message you intend to handle. This allows the handler to validate the message and parse it into an object.

from pydantic import BaseModel, UUID4
from typing import Dict, Optional
from datetime import datetime


class ChatMessage(BaseModel):
    type: str = "chat.message"
    id: UUID4
    thread: UUID4
    sender: UUID4
    content: str
    data: Optional[Dict] = {}
    created: datetime

Create a message handler.

This will first validate and parse a message that matches handled_types using the corresponding entry in models. It will then execute the method specified in handled_types, passing the newly parsed message object.

from channels_handlers.handlers import MessageHandler
# For async, import AsyncMessageHandler


class ChatHandler(MessageHandler):
    namespace = "chat"
    models = {
        "chat.message": ChatMessage,
    }

    def message(self, message):
        # Some logic with message, e.g. save to database
        pass

Import ConsumerHandlerMixin and add it to your Django Channels consumer. Then, add your custom handler to the consumer's handler_classes.

from channels_handlers.consumers import ConsumerHandlerMixin
# For async, import AsyncConsumerHandlerMixin
from channels.generic.websocket import JsonWebsocketConsumer


class MyConsumer(ConsumerHandlerMixin, JsonWebsocketConsumer):
    handler_classes = [ChatHandler]

Compatibility

django-channels-handlers is compatible with Python 3.7+, Django 2.2+, and Django Channels 2.2+.

License

django-channels-handlers is licensed under the MIT License.

Authors

See AUTHORS.md.

Contributing

django-channels-handlers relies on the contributions of talented coders like you. See CONTRIBUTING.md for more information.

Metadata

Release files for django-channels-handlers 0.2.2

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-handlers 0.2.2
File Size Uploaded Provenance
django-channels-handlers-0.2.2.tar.gz 4.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-channels-handlers 0.2.2
File Interpreter ABI Platform
django_channels_handlers-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.6 kB

Release files / django-channels-handlers-0.2.2.tar.gz

Download URL django-channels-handlers-0.2.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
898edde81d5e3cbdca0e5af0fe7a52bb037dba3e769ad2c5ab422e5545901dfb
BLAKE2b-256 checksum
How to use checksums
1abcb8d400abe8e360ecbc8a3c557f4fc03f14712d0c3e01792039c88296a726
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.5 Darwin/19.6.0

Release files / django_channels_handlers-0.2.2-py3-none-any.whl

Download URL django_channels_handlers-0.2.2-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
379b3ebcc26f613b722ef476b785da846cb7ff08a397d909cf75949138f15143
BLAKE2b-256 checksum
How to use checksums
1977a4aba9f93ac932a619ebb0cc8d944dcf9bb1d10040380be65705409f6bfb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.5 Darwin/19.6.0

Security for django-channels-handlers 0.2.2

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