Channels: Websockets fail to work after upgrading to 1.0.0

Created on 9 Jan 2017  路  6Comments  路  Source: django/channels

Hello,

I have just upgraded to channels 1.0.0 (pip install channels --upgrade) and now the websockets fail to work. Everything works fine with channels 0.17.3 and daphne 0.15.0. Are there any code changes I need to make after upgrading?

Here is my pip list:

asgi-redis (1.0.0)
asgiref (1.0.0)
attrs (16.3.0)
autobahn (0.17.1)
backports.ssl-match-hostname (3.5.0.1)
cffi (1.9.1)
channels (1.0.0)
constantly (15.1.0)
cryptography (1.7.1)
daphne (1.0.0)
Django (1.10.5)
django-sslserver (0.19)
djangorestframework (3.5.3)
enum34 (1.1.6)
idna (2.2)
incremental (16.10.1)
ipaddress (1.0.17)
msgpack-python (0.4.8)
Pillow (3.4.2)
pip (9.0.1)
pyasn1 (0.1.9)
pyasn1-modules (0.0.8)
pycparser (2.17)
pyOpenSSL (16.2.0)
pypiwin32 (219)
redis (2.10.5)
reportlab (3.3.0)
service-identity (16.0.0)
setuptools (32.3.1)
six (1.10.0)
Twisted (16.6.0)
txaio (2.6.0)
websocket-client (0.40.0)
wheel (0.30.0a0)
zope.interface (4.3.3)

I start daphne and runworker this way:

daphne -b 0.0.0.0 webapp.asgi:channel_layer -v2
python manage.py runworker -v2

Debug info daphne:

2017-01-09 17:41:16,575 DEBUG    Upgraded connection http.response!ZYbuijREWUGZ to WebSocket websocket.send!fwqnBkXtTurI
2017-01-09 17:41:18,000 WARNING  dropping connection to peer tcp4:127.0.0.1:57462 with abort=True: WebSocket opening handshake timeout (peer did not finish the opening handshake in time)
2017-01-09 17:41:18,000 DEBUG    WebSocket closed for websocket.send!EWpstfxCRPqe

Debug info runworker:

2017-01-09 17:41:16,575 - DEBUG - worker - Got message on websocket.connect (reply websocket.send!fwqnBkXtTurI)
2017-01-09 17:41:16,575 - DEBUG - runworker - websocket.connect
2017-01-09 17:41:16,575 - DEBUG - worker - Dispatching message on websocket.connect to webapp.consumers.ws_connect
2017-01-09 17:41:18,000 - DEBUG - worker - Got message on websocket.disconnect (reply websocket.send!EWpstfxCRPqe)
2017-01-09 17:41:18,000 - DEBUG - runworker - websocket.disconnect
2017-01-09 17:41:18,000 - DEBUG - worker - Dispatching message on websocket.disconnect to webapp.consumers.ws_disconnect

I'm running Windows 10, python 2.7.12.

Most helpful comment

Have you looked at the release notes? http://channels.readthedocs.io/en/latest/releases/1.0.0.html

You need to change your connect consumers after the upgrade to either send back {"accept": True} or send something, otherwise the websocket will not be opened now. Sorry we couldn't get a more elegant upgrade path in place, but the way the design change works makes it almost impossible.

All 6 comments

Have you looked at the release notes? http://channels.readthedocs.io/en/latest/releases/1.0.0.html

You need to change your connect consumers after the upgrade to either send back {"accept": True} or send something, otherwise the websocket will not be opened now. Sorry we couldn't get a more elegant upgrade path in place, but the way the design change works makes it almost impossible.

Awesome! It works. I'll go through the release notes next time. 馃憤

Thanks Andrew!

Hi @andrewgodwin
Apologize for my little knowledge of channels, but in the example in the documentantion, where this change is needed?

// Note that the path doesn't matter for routing; any WebSocket
// connection gets bumped over to WebSocket consumers
socket = new WebSocket("ws://" + window.location.host + "/chat/");
socket.onmessage = function(e) {
    alert(e.data);
}
socket.onopen = function() {
    socket.send("hello world");
}
// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();

@njordr You have to make a change in consumers.py, not in the front-end.

See the example here: http://channels.readthedocs.io/en/latest/generics.html#websockets
You need to add something like self.message.reply_channel.send({"accept": True}) in your connect method to accept the WebSocket connection.

Many thanks :)

Was this page helpful?
0 / 5 - 0 ratings