Skip to content
Django Channels consumers, without the Pain πŸ’Š
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
channels_handlers Automatically execute handler Aug 10, 2019
tests Initial code dump Aug 9, 2019
.coveragerc Initial code dump Aug 9, 2019
.editorconfig Initial code dump Aug 9, 2019
.gitignore Initial code dump Aug 9, 2019
.pre-commit-config.yaml Initial code dump Aug 9, 2019
.travis.yml Add missing dependencies Aug 9, 2019
AUTHORS.rst Initial code dump Aug 9, 2019
CONTRIBUTING.rst
LICENSE Initial code dump Aug 9, 2019
Pipfile Install publishing dependencies Aug 10, 2019
Pipfile.lock Install publishing dependencies Aug 10, 2019
README.rst Add more documentation Aug 10, 2019
pyproject.toml Initial code dump Aug 9, 2019
setup.cfg Fix tests Aug 10, 2019
setup.py Fix install Aug 10, 2019
tox.ini Fix tests Aug 10, 2019

README.rst

django-channels-handlers

Latest PyPI version https://travis-ci.com/joshua-s/django-channels-handlers.svg?branch=master

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.2
  • pydantic>=0.32

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"
    handled_types = {
        "chat.message": "receive_message",
    }
    models = {
        "chat.message": ChatMessage,
    }

    def receive_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.6+, Django 2.1+, and Django Channels 2.2+.

License

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

Authors

django-channels-handlers was written by Josh Smith.

You can’t perform that action at this time.