pythondjangoamazon-web-servicesdjango-channelsdaphne

Other users cannot connect to the server on the websocket


I have a server on AWS EC2 with port 8001 open with a websocket application. The application itself is written in Django Channels and uses a daphne server.

The problem is that I myself can connect to the websocket server, but other users do not have such an opportunity for some reason

The script I'm using to connect

const socket = new WebSocket("ws://54.145.126.99:8001/ws/chat/first_user=19628&second_user=19629", ["Bearer", token]);

socket.onopen = function () {
   console.log("✅ connected to WebSocket server");
};

connection to websock local application use http://127.0.0.1:5173/, application that use frontend dev also use http://localhost:5173/ and runs on React.

I don't know what further info i need to provide so that you can understand problem, because i have no idea where that error could be...


Solution

  • if you're running Daphne like this:

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

    It will only bind to localhost. Change it to:

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

    or

    daphne myproject.asgi:application -p 8001
    

    this makes it accessible to external users.

    also ensure your settings.py allows external connections:

    ALLOWED_HOSTS = ["54.145.126.99", "yourdomain.com", "localhost"]