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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.