Hi, I created a websocket to display real time data, everything is working so far, but I encountered a problem, every time there was a new data, it was sending it to all users, so I installed and tried to configure django-channels-presence, but I'm facing some difficulties to configure it if someone can help me 🙂
Here are my consumers
class OrderConsumer(ListModelMixin, GenericAsyncAPIConsumer):
def get_queryset(self, **kwargs):
return Order.objects.filter(ascount=self.scope["user"])
serializer_class =OrderSerializer
permissions = (permissions.AllowAny,)
async def connect(self, **kwargs):
await self.accept()
await self.room_connect()
await self.model_change.subscribe()
async def disconnect(self, close_code):
await self.room_disconect()
@database_sync_to_async
async def room_connect(self, **kwargs):
Room.objects.add("some_room", self.channel_name, self.scope["user"])
@database_sync_to_async
async def receive(self, text_data=None, bytes_data=None):
if text_data == '"heartbeat"':
Presence.objects.touch(self.channel_name)
@database_sync_to_async
async def room_disconect(self, **kwargs):
Room.objects.remove("some_room", self.channel_name)
@model_observer(Order)
async def model_change(self, message, observer=None, **kwargs):
await self.send_json(message)
@model_change.serializer
def model_serialize(self, instance, action, **kwargs):
return dict(data=OrderSerializer(instance=instance).data, action=action.value)