How to implement consumer for voice chat app on Django?

I am currently working on a voice chat app as a personal project and this app will be like Discord, where users can hop into a chat room and start talking to people in that room. I am only going to make it voice only for now (I will implement text messaging later). I have been looking at examples on Github and I am having a little trouble with how to make the Consumer.

Here is what I have right now:

import json

from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer

class VoiceConsumer(WebsocketConsumer):

    def connect(self):

        self.room_name = self.scope['url_route']['kwargs']['room_name']

        self.room_group_name = "chat_%s" % self.room_name

        # join the room
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name, self.channel_name
        )

        self.accept()

    def disconnect(self, closed_code):

        async_to_sync(self.channel_layer.group_discard)(
            self.room_group_name, self.channel_name
        )

    def receive(self, text_data):

        text_data_json = json.loads(text_data)

        # Ask about this, how to receive voice ?
        voice_message = text_data_json['message']

        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name, {'type' : 'chat_message', 'message': voice_message}
        )

I am currently working on a voice chat app as a personal project and this app will be like Discord, where users can hop into a chat room and start talking to people in that room. I am only going to make it voice only for now (I will implement text messaging later). I have been looking at examples on Github and I am having a little trouble with how to make the Consumer.

Here is what I have right now:

import json

from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer

class VoiceConsumer(WebsocketConsumer):

    def connect(self):

        self.room_name = self.scope['url_route']['kwargs']['room_name']

        self.room_group_name = "chat_%s" % self.room_name

        # join the room
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name, self.channel_name
        )

        self.accept()

    def disconnect(self, closed_code):

        async_to_sync(self.channel_layer.group_discard)(
            self.room_group_name, self.channel_name
        )

    def receive(self, text_data):

        text_data_json = json.loads(text_data)

        # Ask about this, how to receive voice ?
        voice_message = text_data_json['message']

        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name, {'type' : 'chat_message', 'message': voice_message}
        )

As you see, I am confused about the text_data_json[‘message’] part. This is based off an example I have seen where someone implements messages, but I want to implement voice only. What do I change about this to where I can implement voice only ?

text_data_json = json.loads(text_data)
   
# Ask about this, how to receive voice ?
voice_message = text_data_json['message']
    
async_to_sync(self.channel_layer.group_send)(
              self.room_group_name, {'type' : 'chat_message', 'message': voice_message}
            )

I haven’t seen any examples that do this for voice only. I really appreciate the help.

You might be able to do that, I wouldn’t ever recommend it though. I know I would never want to try. (There’s going to be a whole host of issues around negotiating protocols, formats, etc.)

You’re a lot better off integrating WebRTC into your application to handle streaming voice and/or video between two browsers.

Thanks for the reply ! This is very interesting that you mentioned it, but it seems like this guy’s implementation with WebRTC is extremely similar to mine:

It’s probably similar to mine as well. There are only so many ways you should configure WebRTC.

Note that they’re not passing the audio data through the Django server. They’re configured to use a third-party STUN/TURN server when necessary as well.