# How to implement consumer for voice chat app on Django?

**URL:** <https://forum.djangoproject.com/t/how-to-implement-consumer-for-voice-chat-app-on-django/19288>\
**Category:** Getting Started\
**Created:** [March 5, 2023, 6:30pm UTC](https://forum.djangoproject.com/t/how-to-implement-consumer-for-voice-chat-app-on-django/19288 "2023-03-05T18:30:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KTEngineer91](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ktengineer91/32/12529_2.png) [@KTEngineer91](https://forum.djangoproject.com/u/KTEngineer91)\
**Post date:** [March 5, 2023, 6:30pm UTC](https://forum.djangoproject.com/t/how-to-implement-consumer-for-voice-chat-app-on-django/19288/1 "2023-03-05T18:30:56Z")

</div>

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:

```auto
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:

```auto
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 ?

```auto
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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 5, 2023, 6:34pm UTC](https://forum.djangoproject.com/t/how-to-implement-consumer-for-voice-chat-app-on-django/19288/2 "2023-03-05T18:34:02Z")

</div>

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](https://webrtc.org/) into your application to handle streaming voice and/or video between two browsers.

---

<div class="post-metadata">

**Author:** ![KTEngineer91](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ktengineer91/32/12529_2.png) [@KTEngineer91](https://forum.djangoproject.com/u/KTEngineer91)\
**Post date:** [March 5, 2023, 8:41pm UTC](https://forum.djangoproject.com/t/how-to-implement-consumer-for-voice-chat-app-on-django/19288/3 "2023-03-05T20:41:18Z")

</div>

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:

> **[GitHub - vsjakhar/Django-WebRtc: Video and Audio Calling with WebRtc in Django](https://github.com/vsjakhar/Django-WebRtc)**
>
> Video and Audio Calling with WebRtc in Django. Contribute to vsjakhar/Django-WebRtc development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 5, 2023, 8:53pm UTC](https://forum.djangoproject.com/t/how-to-implement-consumer-for-voice-chat-app-on-django/19288/4 "2023-03-05T20:53:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ArSiddharth](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/arsiddharth/32/18796_2.png) [@ArSiddharth](https://forum.djangoproject.com/u/ArSiddharth)\
**Post date:** [February 5, 2024, 2:34am UTC](https://forum.djangoproject.com/t/how-to-implement-consumer-for-voice-chat-app-on-django/19288/5 "2024-02-05T02:34:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ArSiddharth](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/arsiddharth/32/18796_2.png) [@ArSiddharth](https://forum.djangoproject.com/u/ArSiddharth)\
**Post date:** [February 5, 2024, 2:38am UTC](https://forum.djangoproject.com/t/how-to-implement-consumer-for-voice-chat-app-on-django/19288/6 "2024-02-05T02:38:36Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 5, 2024, 2:50am UTC](https://forum.djangoproject.com/t/how-to-implement-consumer-for-voice-chat-app-on-django/19288/7 "2024-02-05T02:50:08Z")

</div>

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](https://pragprog.com/titles/ksrtc/programming-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.
