Closed Randix6644 closed 11 months ago
@Randix6644 could you link to the example in question.
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)
what are groups_for_signal and groups_for_consumer for? I've read the documents but still have no clue about them. And I don't get what " f'chat{instance}'" means in the example, what does "" mean in this case?