How to send message on django channel to all users before disconnect

Viewed 158

i am trying to send a message to all users before a client disconnects(when client leaves the page).

    def disconnect(self, close_code):
        # Leave room group
        self.user = self.scope["user"]
        self.send(text_data=json.dumps({
            "message": self.user.username,
            "typeMessage": "lobby",
            "created": False,
        }))
        print("Try to send the message")
        async_to_sync(self.channel_layer.group_discard)(
            self.group_name,
            self.channel_name
        )

This is the function where i try to send the message but it doesn't work. The print function works, "Try to send the message" is displayed on terminal's output. If you could help me with a suggestion that would be great. Thank you!

0 Answers
Related