I have a django application that uses django channels in order to inform clients something new has happened. basically i have a third side application that triggers my django applications endpoint called notify. every time this notify endpoint is triggered I want to send a message via socket to all clients in certain group. The flow works fine, the problem is that each time I use channel_layer.group_send() a new connection is opened between my django application instance and my redis server.
this creates an insane amount of connections to my redis server until it reaches its max connection capacity. I cant seem to understand why my django application opens so much connections, any idea?
from rest_framework.response import Response
from adrf.viewsets import ViewSet
from rest_framework.status import HTTP_201_CREATED
from channels.layers import get_channel_layer
class NotifyViewSet(ViewSet):
channel_layer = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if NotifyViewSet.channel_layer is None:
NotifyViewSet.channel_layer = get_channel_layer()
self.channel_layer = NotifyViewSet.channel_layer
async def create(self, request):
await self.channel_layer.group_send("example",
{
"type" : "chat_message",
"message" : "hello",
}
)
return Response(HTTP_201_CREATED)