Deploy django-channels to render.com

I implemented django-channels and daphne, and it works locally based on this tutorial :
Tutorial — Channels 3.0.3 documentation

However, I have difficulties to deploy the application to render based on these guidelines
Deploying — Channels 3.0.3 documentation

I have set the following start command

daphne my_app_name.asgi:application

Here is asgi.py

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app_name.settings")
django.setup()
from channels.http import AsgiHandler
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import chat.routing
application = ProtocolTypeRouter({
    "http": AsgiHandler(),
     "websocket": AuthMiddlewareStack(
         URLRouter(
            chat.routing.websocket_urlpatterns
        )
    ),
})

And here is the error returned when I try to deploy :

Mar 12 10:50:39 AM  2021-03-12 10:50:39,398 INFO     Starting server at tcp:port=8000:interface=127.0.0.1
Mar 12 10:50:39 AM  2021-03-12 10:50:39,399 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Mar 12 10:50:39 AM  2021-03-12 10:50:39,399 INFO     Configuring endpoint tcp:port=8000:interface=127.0.0.1
Mar 12 10:50:39 AM  2021-03-12 10:50:39,400 INFO     Listening on TCP address 127.0.0.1:8000

But the deployment cannot be finalized (it runs indefinetely).

Any idea what I am doing wrong? Is there a problem with the IPs ? Django-channels guidelines indicate we can also specify the port and IP that Daphne binds to. e.g. :

daphne -b 0.0.0.0 -p 8001 myproject.asgi:application

Thanks a lot

Hey @jpuaux, I think you may have answered your own question! Render services should bind to 0.0.0.0 (all available network interfaces) rather than 127.0.0.1 (localhost). Let us know if you continue to see issues after making that change.

It is working with the following command :

daphne -b 0.0.0.0 pcaexpress.asgi:application

Thanks a lot for your answer.

1 Like

is there any way to use these both commands

gunicorn project.wsgi
daphne -b 0.0.0.0 project.asgi:application

because I am using this command: daphne -b 0.0.0.0 project.asgi:application, I am getting an error in google login but when my
starting command was: gunicorn project.wsgi, google login works perfectly.

Hey there - this isn’t a Render issue.

You’re getting an Error 400 there from Google - the redirect URL you are requesting is different to what the oAuth client on Google is configured with so Google rejects the request.

John B

Hi im dealing with this issue too. I also need to run daphne and gunicorn because with daphne i can not use csrf. So, i have the same question, how can i run both gunicorn and daphne? Thank you!

try running this if you can access the terminal

pip install -U 'Twisted[tls,http2]'

if you can access the terminal, add Twisted[tls,http2] to the bottom of your requirements.txt

hey please i have same problem when my start command is daphne -b 0.0.0.0 root.asgi:application i get an endless deployment sort of loop that never stops deploying. if i change it to this daphne chat.asgi:application --port $PORT --bind 0.0.0.0 it deploys but i get This page isn’t workingepschat.onrender.com redirected you too many times.
Try deleting your cookies.
ERR_TOO_MANY_REDIRECTS

Same for me, It is endlessly deploying.

2024-01-01 22:52:49,281 INFO     Starting server at tcp:port=8000:interface=0.0.0.0
2024-01-01 22:52:49,282 INFO     HTTP/2 support enabled
2024-01-01 22:52:49,282 INFO     Configuring endpoint tcp:port=8000:interface=0.0.0.0
2024-01-01 22:52:49,282 INFO     Listening on TCP address 0.0.0.0:8000

Try replacing the port number you use with the default one Render uses when deploying an app. I think it’s 10000.
So instead of daphne --port 8000 --bind 0.0.0.0 myproject.asgi:application,
use daphne --port 10000 --bind 0.0.0.0 myproject.asgi:application.
This worked for me.