Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17100

channel_layer.send() not sending messages to unique channel_names based on username - Django channels

$
0
0

I am trying to implement a way to send messages to users that are user specific. I have seen the docs where they advice to store the channel_name in the database and remove it on disconnect, but I think that will be a burden on the database as channel_name keeps on changing whenever the user connects to the consumer. So I tried the below method during accept, where I tried to assign the user's user_name as a unique channel name

consumer

class ChatGenericAsyncConsumer(AsyncWebsocketConsumer):"""this is an async consumer"""    async def connect(self):        self.user = self.scope["user"]        if self.user.is_authenticated:            print(                f"authentication successful connection accepted for {self.user.user_name}"            )            self.username = f"{self.user.user_name}"            self.channel_name = f"{self.user.user_name}"            await self.accept()        else:            print("authentication unsuccessful connection closed")            await self.close(code=4123)    async def receive(self, text_data=None, bytes_data=None):        pass    async def disconnect(self, code):        await self.close(code=4123)    # this is the event handler of 'chat.message'    async def chat_message(self, event):"""        this method handles the sending of message        to the group.        this is same as chat.message"""        # sending message to the group        print(event["data"])        await self.send(text_data=event["data"])

and then tried to send a message from outside the consumer, where I try to mimic a situation that for some reason the admin wants to send a user specific message or notification to a specific user. So I coded the below api and used channel_layer.send() to send message to that specific channel name

api

@api_view(["POST"])@permission_classes([AllowAny])def send_message_from_admin(request, group_name):    try:        message = request.data.get("message")        username = request.data.get("username")        channel_layer = get_channel_layer()        send_data = {"user": "Admin", "message": message}        async_to_sync(channel_layer.send)(            username, {"type": "chat.message", "data": json.dumps(send_data)}        )        return Response(            {"message": "sent message"}, status=status.HTTP_200_OK        )    except Exception as e:        print(f"An exception occurred {e}")        return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

here the username is the same as the channel_name set during connect. But somehow this does not work and the event handler is not called to send the message to the specific channel name and the user does not receive the message.

Please suggest me whether there is something wrong with the code or my understanding of sending user specific messages or notifications


Viewing all articles
Browse latest Browse all 17100

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>