Giter Site home page Giter Site logo

need more documents for groups_for_signal and groups_for_consumer, and what "__" means in the examples about djangochannelsrestframework HOT 2 CLOSED

Randix6644 avatar Randix6644 commented on June 12, 2024
need more documents for groups_for_signal and groups_for_consumer, and what "__" means in the examples

from djangochannelsrestframework.

Comments (2)

hishnash avatar hishnash commented on June 12, 2024

@Randix6644 could you link to the example in question.

from djangochannelsrestframework.

johnthagen avatar johnthagen commented on June 12, 2024

I assume @Randix6644 is referring to the examples in the README here:

# consumers.py
from djangochannelsrestframework.consumers import AsyncAPIConsumer
from djangochannelsrestframework.decorators import action
from djangochannelsrestframework.observer import observer
from rest_framework import status
from .signals import joined_chat_signal
from .serializers import UserSerializer


class TestConsumer(AsyncAPIConsumer):

    @action()
    def join_chat(self, chat_id, **kwargs):
        serializer = UserSerializer(instance=self.scope['user'])
        joined_chat_signal.send(sender='join_chat', data=serializer.data, **kwargs)
        return {}, status.HTTP_204_NO_CONTENT

    @observer(signal=joined_chat_signal)
    async def joined_chat_handler(self, data, observer=None, action=None, subscribing_request_ids=[], **kwargs):
        for request_id in subscribing_request_ids:
            await self.reply(action='joined_chat', data=data, status=status.HTTP_200_OK, request_id=request_id)

    @joined_chat_handler.serializer
    def join_chat_handler(self, sender, data, **kwargs): # the data comes from the signal.send and will be available in the observer
        return data

    @joined_chat_handler.groups_for_signal
    def joined_chat_handler(self, instance, **kwargs):
        yield f'chat__{instance}'

    @joined_chat_handler.groups_for_consumer
    def joined_chat_handler(self, chat, **kwargs):
        if chat:
            yield f'chat__{chat}'

    @action()
    async def subscribe_joined(self, chat_id, request_id, **kwargs):
        await self.joined_chat_handler.subscribe(chat_id, request_id=request_id)

from djangochannelsrestframework.

Related Issues (20)

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.