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.

1 Like

Hi @KenWhitesell,

I am working on a Django project which involves chat and voice calls. As we cannot use regular phone calls, I am looking to implement a voice-calling feature in the project.

Could you please share any relevant resources, tutorials, or GitHub projects to help me with this?

Thank you.

I have tried many projects on GitHub but all have shown errors or did not work properly. (sometimes shows setup error and sometimes does not connect with server)

I am not aware of any generally available public resources for this that are current enough to be useful.

One of the issues that you will encounter is that the standards have evolved over time, and sample code that you find from 5+ years ago won’t work now without modifications.

I bought the beta version of the book WebRTC, it’s what I used to get started.

Also keep in mind that Django’s involvement in WebRTC is very limited. Most of what and how it works is directly between the browsers involved, except when a STUN & TURN server is needed to establish a connection due to networking issues such as firewalls.

1 Like