Websocket server is started but not connecting to it after defining port.

space-we3TRIAL

a year ago

I am now deplying django-channels application for which I need to start asgi server to make channels work but When I set command in Procfile

web: daphne .asgi:application

then server is successfully started and showing

2024-01-07 08:44:25,699 INFO     Starting server at tcp:port=7097:interface=0.0.0.0

2024-01-07 08:44:25,699 INFO     HTTP/2 support enabled

2024-01-07 08:44:25,699 INFO     Configuring endpoint tcp:port=7097:interface=0.0.0.0

2024-01-07 08:44:25,700 INFO     Listening on TCP address 0.0.0.0:7097

but when I visit the website and try to connect with websocket like

const socket = new WebSocket(wss://:______.com:7097/ws/endpoint/);

then it is not connecting to websocket even after defining the port with wss.

Solved

8 Replies

a year ago

Try to not specify the port, so do:

const socket = new WebSocket(wss://:______.com/ws/endpoint/);

Status changed to Solved railway[bot] over 1 year ago


space-we3TRIAL

a year ago

Thanks , I tried without port then at least it showed some response regarding websocket in server but when tried with wscat

wscat -c wss://___.com/ws/endpoint/

it is showing in console

error: Unexpected server response: 403

and in logs

- - [07/Jan/2024:11:35:10] "WSCONNECTING /ws/endpoint/" - -

 - - [07/Jan/2024:11:35:10] "WSREJECT /ws/endpoint/" - -

 - - [07/Jan/2024:11:35:10] "WSDISCONNECT /ws/endpoint/" - -

Is this problem with my app? Or Is it some problem in configuration? Because websocket is working with localhost.


a year ago

it's likely an issue with your app, do you have your allowed origins configured correctly?


space-we3TRIAL

a year ago

Yeah, Every request from server is working fine except for websockets. No error and just Disconnect


space-we3TRIAL

a year ago

@dev It worked. Thanks for your Help. I will update "How it worked" here soon.

Edit:
In the configuration of my Django app's asgi.py file, I had implemented AllowedHostsOriginValidator around the URLRouter. This was done to enhance the security of the websocket connections by restricting the origins and hosts that are allowed to connect. However, I temporarily removed the AllowedHostsOriginValidator (although it is strongly advised NOT TO DO so) in order to test if it was causing any issues. After confirming that was what was causing the websocket to disconnect, I promptly reinstated the AllowedHostsOriginValidator to wrap the URLs once again and also defined a list of allowed origins.

Read more here


a year ago

Awesome, glad it worked out


space-we3TRIAL

a year ago

I believe this problem would be difficult to resolve without your assistance.


a year ago

Thank you, I'm happy I could help :)