Giter Club home page Giter Club logo

django-channels-handlers's Introduction

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.

django-channels-handlers's People

Contributors

dependabot[bot] avatar ferndot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

django-channels-handlers's Issues

Project Status?

I noticed the last commit was around 3 years ago. Is this project still maintained or would it not be a good fit for new usage?

Automated tests

To ensure quality, all code in this package should have unit tests.

Better documentation

This project needs comprehensive documentation. Optimally, it would be stored on ReadTheDocs.

Fully use namespace and autogenerate methods

For simplicity, messages could be of the format 'namespace.method'. That would allow the handler to automatically determine:

  1. Whether it can handle the message
  2. What method it should fire

I am also open to other ideas and ways to make this better.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.