Channels: Websocket connection failed in production

Created on 4 Dec 2016  Â·  22Comments  Â·  Source: django/channels

Hello,

I try to use Django Channels on my website developed with Django 1.10 and Python 3.5. This website is hosted on Webfaction.
My problem is about the socket, the connection doesn't works, I have this error when I go to the home page of the website :

WebSocket connection to 'ws://mywebsite.com/' failed: Error during WebSocket handshake: Unexpected response code: 200
Disconnected from chat socket

I created the websocket in a Javascript file :

var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var ws_path = ws_scheme + '://' + window.location.host + window.location.pathname;                  
var socket = new WebSocket(ws_path);

I have already configure Redis server and it works :

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 111111)],
        },
    "ROUTING": "projup.routing.channel_routing",
    },
}

_111111 is the port number of an application._

My ALLOWED_HOST configuration is :

ALLOWED_HOSTS = [
        '.mywebsite.com',
        '.mywebsite.net',
]

I don't have error logs.

How can I resolve this problem (websocket connection impossible) ?
Do you need a new app/port for the websocket ?
Should I use the port of my website ? How ?
Do you need to run a command that would start listening on a port ?

Thank you ! :)

All 22 comments

If you don't have error logs I'm afraid there's not much I can help you with. A response code 200 means that something in the chain, be it webfaction routing or a webserver you're running, doesn't support websockets.

In future, you can ask requests like this on the django-users mailing list.

I am sure that webfaction supports websocket but I don't know how can I do...

Can you describe me the steps for use Django-Channels in production ?

Hi,
You can use daphne server to manage http/ws or only ws connections. In both cases you will need a webfaction application of type "Custom websockets app (listening on port)" created.
Once you have created the app take note of assigned port number to the app.
Then when you invoke daphne, use that port number, ie:
daphne -p {port_number} {route:to:asgi:module:channel_layer}
Hope it helps.

Thank you for your help I resolved my problem, but can you confirme if that
I did is correct ? :

  • I create the Custom websockets app (listening on port),
  • I add this custom app to the Django app (my website) with the subdomain :
    ".mywebsite.com/sart",
  • I run the command "daphne -p {port_number} {route:to:asgi:module:channel_
    layer}",
  • The path of the websocket is "ws://mywebsite.com/sart".

It is correct ?
So now, should I need a process that runs in background (with daemon) for
the Daphne command ?

2016-12-05 12:50 GMT+01:00 Jorge Rodríguez-Flores Esparza <
[email protected]>:

Hi,
You can use daphne server to manage http/ws or only ws connections. In
both cases you will need a webfaction application of type "Custom
websockets app (listening on port)" created.
Once you have created the app take note of assigned port number to the app.
Then when you invoke daphne, user that port number, ie:
daphne -p {port_number} {route:to:asgi:module:channel_layer}
Hope it helps.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/django/channels/issues/445#issuecomment-264835370,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AWPdCHkkGlhVop2lbKMrQlt2c7-G5YStks5rE_qIgaJpZM4LDqtf
.

I'm not that familiar with Webfaction, but I know you'll at least need another process that runs runworker as well.

Ok thank you very much for your help for your package (I am very happy to use it) !

I resolved the problem, I didn't run Daphne so the websocket connection didn't works... It's my fault, sorry.

hey Ya2s , wanted to know if you could elaborate on how you finally got it to work , I myself have run into a problem very similiar I am using webfation as well i wanted to test out django channels to use the websocket ability in a project im making.
heres my error:

(index):172 Disconnected from chat socket
websocketbridge.js:118 WebSocket connection to 'ws://www.openchat.us/chat/stream/' failed: Error during WebSocket handshake: Unexpected response code: 404
connect @ websocketbridge.js:118
(index):172 Disconnected from chat socket
websocketbridge.js:118 WebSocket connection to 'ws://www.openchat.us/chat/stream/' failed: Error during WebSocket handshake: Unexpected response code: 404
connect @ websocketbridge.js:118
(index):172 Disconnected from chat socket

I was looking at your previous comments when you asked about these steps: - I run the command "daphne -p {port_number} {route:to:asgi:module:channel_
layer}",

  • The path of the websocket is "ws://mywebsite.com/sart".

did you enter that command in webfaction ussing putty ssh : "daphne -p {port_number} {route:to:asgi:module:channel_
layer}",
what im putting is daphne -p {port #} myproject.asgi:channel_layer
but I get an error as well

one other question would i need to put my web address in the interface because in the shell i see this
Starting server at tcp:port=26358:interface=127.0.0.1, channel_layer myproject.asgi:channel_layer

thats what im seeing in the shell
but the error that im seeing in google tools is

(index):172 Disconnected from chat socket
websocketbridge.js:118 WebSocket connection to 'ws://www.openchat.us/chat/stream/' failed: Error during WebSocket handshake: Unexpected response code: 404
connect @ websocketbridge.js:118
www.openchat.us/:172 Disconnected from chat socket

failed: WebSocket is closed before the connection is established.
thats my current errro that im getting now

Hello,

I simply used the answer of jorette :

Hi,
You can use daphne server to manage http/ws or only ws connections. In both cases you will need a webfaction application of type "Custom websockets app (listening on port)" created.
Once you have created the app take note of assigned port number to the app.
Then when you invoke daphne, use that port number, ie:
daphne -p {port_number} {route:to:asgi:module:channel_layer}
Hope it helps.

And I run the 3 commands :

daphne -e ssl::privateKey=:certKey= .asgi:channel_layer -p -b 0.0.0.0

python3.5 manage.py runworker

./redis-server 127.0.0.1:

My Daphne command is due to I am using SSL !

Hope it helps !

I see since I'm using webfaction and have to use putty would I need to open
3 or 4 consoles to enter the 4 commands that you said and since I am using
putty would I need to enter the key as well thanks

On May 22, 2017 4:55 AM, "Ya2s" notifications@github.com wrote:

Hello,

I simply used the answer of jorette :

Hi,
You can use daphne server to manage http/ws or only ws connections. In
both cases you will need a webfaction application of type "Custom
websockets app (listening on port)" created.
Once you have created the app take note of assigned port number to the app.
Then when you invoke daphne, use that port number, ie:
daphne -p {port_number} {route:to:asgi:module:channel_layer}
Hope it helps.

And I run the 3 commands :

daphne -e ssl::privateKey=:certKey=
.asgi:channel_layer -p -b 0.0.0.0

python3.5 manage.py runworker

./redis-server 127.0.0.1:

My Daphne command is due to I am using SSL !

Hope it helps !

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/django/channels/issues/445#issuecomment-303054603,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ARSyoCTBN9aDPpHls1FpjnFGYnM1V0Ugks5r8VuGgaJpZM4LDqtf
.

another question what do you have for your settings.py

for the redis configuration do you have the port as is 6379 or do you put
your port from wefaction in there as well thanks

CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [(redis_host, 6379)],
},
"ROUTING": "myproject.routing.channel_routing",
},
}

On Mon, May 22, 2017 at 4:55 AM, Ya2s notifications@github.com wrote:

Hello,

I simply used the answer of jorette :

Hi,
You can use daphne server to manage http/ws or only ws connections. In
both cases you will need a webfaction application of type "Custom
websockets app (listening on port)" created.
Once you have created the app take note of assigned port number to the app.
Then when you invoke daphne, use that port number, ie:
daphne -p {port_number} {route:to:asgi:module:channel_layer}
Hope it helps.

And I run the 3 commands :

daphne -e ssl::privateKey=:certKey=
.asgi:channel_layer -p -b 0.0.0.0

python3.5 manage.py runworker

./redis-server 127.0.0.1:

My Daphne command is due to I am using SSL !

Hope it helps !

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/django/channels/issues/445#issuecomment-303054603,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ARSyoCTBN9aDPpHls1FpjnFGYnM1V0Ugks5r8VuGgaJpZM4LDqtf
.

@djangorobert The issue comments aren't for user help - please keep to the django-users mailing list, thanks.

Lol will do

On May 22, 2017 1:28 PM, "Andrew Godwin" notifications@github.com wrote:

@djangorobert https://github.com/djangorobert The issue comments aren't
for user help - please keep to the django-users mailing list, thanks.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/django/channels/issues/445#issuecomment-303182122,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ARSyoNIMF5get6c9hlgQsbNZaL254Orwks5r8dPegaJpZM4LDqtf
.

Ya2 feel free to email me on my own email [email protected] thank
you

On Mon, May 22, 2017 at 1:29 PM, Robert librado djangocharm2020@gmail.com
wrote:

Lol will do

On May 22, 2017 1:28 PM, "Andrew Godwin" notifications@github.com wrote:

@djangorobert https://github.com/djangorobert The issue comments
aren't for user help - please keep to the django-users mailing list, thanks.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/django/channels/issues/445#issuecomment-303182122,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ARSyoNIMF5get6c9hlgQsbNZaL254Orwks5r8dPegaJpZM4LDqtf
.

@andrewgodwin I hosted my django app on dreamhost server
I used this document for django channels deployment and followed three steps mentioned in this document.
http://channels.readthedocs.io/en/stable/deploying.html#asgi-alone

here is my channel_layers configurations
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [("my_domain.com", 6379)],
},
"ROUTING": "ProblemSolverCentral.routing.channel_routing",
},
}
initially I was getting error connection refused
I made port 6379 listen by using
nc -l 6379

But I am getting error
WebSocket connection to 'ws://www.problemsolvercentral.com/social_network/1' failed: Error during WebSocket handshake: Unexpected response code: 404

is there any issue in CHANNEL_LAYERS configuration?

Hey, Ya2s,

I am also getting the same errors on console bar.

WebSocket connection to 'ws://localhost:6497/chat/billowing-thunder-7354/' failed: WebSocket is closed before the connection is established.
:6497/static/reconnecting-websocket.min.js:1 Uncaught INVALID_STATE_ERR : Pausing to reconnect websocket

while i develop my project deepend on : https://channels.readthedocs.io/en/latest/deploying.html. the websocket interface always get 404 code , on the other hand , the http-server interface works ok, i promiss that it works normal in develop environment, only get unexpected code 404 in production environment, how to work it out?

hello may i update this ? cause when i deploy this on our server - (digital ocean + nginx), i got a response in console "failed: Error during WebSocket handshake: Unexpected response code: 200"

how can i come up on this?

@Tisoy21 probably we'll need all detail you can provide about data exchanges. This page shows some pointers to get them details. Also all server configurations and logs could help. If you have ssh access you could ask somebody to check for you.

@jpic on my local its working properly, i install redis and django channels but when i deploy it on our server, it gives that 200 response error, how can i configure that ?

maybe the ws:///news

that's my websocket protocol? do i need some stuffs like other domain/host for that ?

I ran into the same issue and got it working after a couple days of pulling my hair out.
The issue is Webfaction-related.

When you create your Django app, you have to use a "Custom websockets app listening on port", and deploy your code there, instead of using a pre-made "Django/Python" app.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

laevilgenius picture laevilgenius  Â·  18Comments

ipartola picture ipartola  Â·  20Comments

DarthLabonne picture DarthLabonne  Â·  33Comments

sachinrekhi picture sachinrekhi  Â·  53Comments

dgilge picture dgilge  Â·  30Comments