0

I'm making a chat server on Django and atm trying to implement websockets. It works fine locally but whenever I launch it on Heroku, websocket is unreachable. Here's my client script:

var loc = window.location
var ws_start = 'ws://'
if (loc.protocol == 'https:'){
    ws_start = 'wss://'
}
var endpoint = ws_start + loc.host + loc.pathname
var socket = new WebSocket(endpoint)

Full error code:

WebSocket connection to 'wss://my-app.herokuapp.com/chat' failed: Error during WebSocket handshake: Unexpected response code: 500

I've seen a variety of similar questions already, and they are either left unanswered or delve into dealing with SSL certificate. There's one answer that would potentially save me (and other folks) alot of frustration if anyone was to confirm it's true. It's quite old and there's no feedback after it was posted: https://stackoverflow.com/a/45173822/7446564.

Lev Slinsen
  • 369
  • 1
  • 4
  • 21

1 Answers1

1

Thanks to Heroku logs, I was able to get the actual error message:

Django daphne asgi: Django can only handle ASGI/HTTP connections, not websocket

This answer helped me fix it: https://stackoverflow.com/a/59909118/7446564

So in conclusion: if you have an error launching websockets on Django, make sure your .asgi file is properly set up. I'll also attach my Procfile below since setting it up first time was also a little journey and I hope it might be helpful as well:

web: daphne my-app.asgi:application --port $PORT --bind 0.0.0.0 -v2

Lev Slinsen
  • 369
  • 1
  • 4
  • 21