1
votes

I am trying to run my first django app on heroku, I managed to successfully deploy the app and everything works besides the web sockets. Locally I used in memory channels and everything worked. I tried on heroku both in memory channels and redis and both does not work. I tried many different configs but I couldn`t figure out any working solution. Also the heroku logs give me no clue what could be wrong..

The error that I get in browser:

join_game.js:222 WebSocket connection to 'wss://battleships-war.herokuapp.com/game/20' failed: Error during WebSocket handshake: Unexpected response code: 404

Javascript client code:

[I tried ws and wss prefixes and both crash ]

var ws = new WebSocket((window.location.protocol == 'http') ? 'ws://' : 'wss://' +  window.location.host + '/game/' + gameId);

redis config in settings.py

 CHANNEL_LAYERS = {
     "default": {
         "BACKEND": "asgi_redis.RedisChannelLayer",
         "CONFIG": {
             "hosts": [('ec2-34-254-133-4.eu-west-1.compute.amazonaws.com', 26849)],
        },
         'ROUTING': 'battleships.routing.channel_routing',
    }, }

optional in memory config which worked locally:

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'asgiref.inmemory.ChannelLayer',
        'ROUTING': 'battleships.routing.channel_routing',
    },
}

redis credentials from heroku:

  • Host: ec2-34-254-133-4.eu-west-1.compute.amazonaws.com
  • User
  • Port 26849
  • Password
  • URI: redis://h:p69877187246d4f0b36a94488814ee0b372234a2e943a61df0cdc84e4f85f35f2@ec2-34-254-133-4.eu-west-1.compute.amazonaws.com:26849

procfile:

web: gunicorn battleships.wsgi --preload

heroku logs:

2019-01-14T21:06:52.847189+00:00 heroku[web.1]: State changed from up to starting 2019-01-14T21:07:00.911326+00:00 heroku[web.1]: Starting process with command gunicorn battleships.wsgi --preload 2019-01-14T21:07:04.629926+00:00 heroku[web.1]: State changed from starting to up 2019-01-14T21:07:06.453341+00:00 heroku[web.1]: Process exited with status 0 2019-01-14T21:07:51.531270+00:00 heroku[router]: at=info method=GET path="/new_game/" host=battleships-war.herokuapp.com request_id=45707e08-cfd9-458c-8ce4-0f423acfb632 fwd="80.49.199.124" dyno=web.1 connect=0ms service=374ms status=200 bytes=1307 protocol=http 2019-01-14T21:07:53.241606+00:00 heroku[router]: at=info method=GET path="/new_game/" host=battleships-war.herokuapp.com request_id=a77411fc-2ab6-4be7-b4c9-e86d9440c3e1 fwd="80.49.199.124" dyno=web.1 connect=0ms service=163ms status=200 bytes=1307 protocol=http 2019-01-14T21:07:57.454852+00:00 heroku[router]: at=info method=POST path="/new_game/" host=battleships-war.herokuapp.com request_id=76aab54e-4508-4485-90de-e66e7bb4715b fwd="80.49.199.124" dyno=web.1 connect=0ms service=31ms status=200 bytes=290 protocol=http 2019-01-14T21:08:01.604730+00:00 heroku[router]: at=info method=GET path="/game/22/dfgdfgdfgdf" host=battleships-war.herokuapp.com request_id=d4c9a31a-0dc6-436a-b35b-054e94770a5e fwd="80.49.199.124" dyno=web.1 connect=0ms service=30ms status=200 bytes=1310 protocol=http 2019-01-14T21:08:02.002519+00:00 heroku[router]: at=info method=GET path="/game/22/dfgdfgdfgdf/join" host=battleships-war.herokuapp.com request_id=3c682a1a-f203-493a-853d-4a4ff9cccaf8 fwd="80.49.199.124" dyno=web.1 connect=0ms service=267ms status=500 bytes=149564 protocol=http 2019-01-14T21:07:03+00:00 app[heroku-redis]: source=REDIS addon=redis-octagonal-92972 sample#active-connections=1 sample#load-avg-1m=0.09 sample#load-avg-5m=0.115 sample#load-avg-15m=0.125 sample#read-iops=0 sample#write-iops=0 sample#memory-total=15664336kB sample#memory-free=11855124kB sample#memory-cached=1580208kB sample#memory-redis=278200bytes sample#hit-rate=0 sample#evicted-keys=0 2019-01-14T21:08:07.295235+00:00 heroku[router]: at=info method=GET path="/game/22/dfgdfgdfgdf" host=battleships-war.herokuapp.com request_id=789356b5-acb3-48e7-9dd1-be5250a7445d fwd="80.49.199.124" dyno=web.1 connect=0ms service=25ms status=200 bytes=1310 protocol=http

Can anyone help me with that or suggest a different approach?

1

1 Answers

0
votes

I don't think gunicorn supports asgi (I'm not sure). But, I did this, and it worked. Keep in mind I am not using channel layers:

asgi.py

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
django.setup()
application = get_default_application()

procfile

web: daphne project.asgi:application --port $PORT --bind 0.0.0.0