Sending a message to a specific group in Django Channels

I would like to connect every user who visits a particular page. Let's call this the, 'Waiting Room' to a websocket.

When the user connects to a websocket they are subscribed to a group. The group is named after the user's user_id. So User1 and User would connect to the websocket and be subscribed to the groups; 1 and 2 respectively.

After the user is connected, I trigger a method that sends a user_id the user has been matched with and room_id to my server. Assuming User1 is matched with User2. User1 would send User2 and room_id to the server. I then send a message back to the group associated with the user_id received. Meaning I send a message from User1's websocket to User2's client.

However, when I implement this logic no messages are received by User2.

This is what I have in consumers.py:

class PracticeConsumer(AsyncConsumer):
    username_id = None
    async def websocket_connect(self, event):
        username = self.scope['user']
        username_id = str(await self.get_user(username))
        group_name = username_id
        await self.channel_layer.group_add(
            '{}'.format(group_name),
            self.channel_name
        )
        await self.send({
            "type": "websocket.accept"
        })

    async def websocket_receive(self, event):
        received = event["text"] 
        invite_data = received.split()
        matched_user = invite_data[0]
        username = invite_data[2]
        user_id = str(await self.get_user(matched_user))
        my_response = {
            "message": "!!!!the websocket is sending this back!!!!"
        }
        sleep(1)
        await self.channel_layer.group_send(
            '{}'.format(user_id),
            {
                "type": "send.message",
                "message": json.dumps(my_response),
                "username": username
            })
...
    async def send_message(self, event):
        message = event['message']
        await self.send({
            'message': message
        })

    @database_sync_to_async
    def get_user(self, user_id):
        try:
            return User.objects.get(username=user_id).pk
        except User.DoesNotExist:
            return AnonymousUser()

The message is logged in my terminal but not actually sent to client. This is the full traceback I see:

    INFO 2022-09-26 10:09:43,129: WebSocket CONNECT /connect/testing/ [127.0.0.1:56653]
    INFO 2022-09-26 10:09:44,239: sending message inside presenter{"message": "!!!!the websocket is sending this back!!!!"}
    INFO 2022-09-26 10:09:44,240: sending message inside presenter{"message": "!!!!the websocket is sending this back!!!!"}
    Exception inside application: 'type'
Traceback (most recent call last):
  File "C:\Users\15512\Desktop\django-project\peerplatform\signup\middleware.py", line 45, in __call__
    return await super().__call__(scope, receive, send)
  ...
  File "C:\Users\15512\anaconda3\lib\site-packages\channels\consumer.py",   File "C:\Users\15512\Desktop\django-project\peerplatform\signup\consumers.py", line 76, in send_message
    await self.send({
  File "C:\Users\15512\anaconda3\lib\site-packages\channels\consumer.py", line 81, in send
    await self.base_send(message)
  File "C:\Users\15512\anaconda3\lib\site-packages\channels\sessions.py", line 186, in send
    message["type"] in self.save_message_types
Back to Top