arminrock
arminrock

Reputation: 535

Django channels websocket.receive is not handled

I'm trying to implement Django channels going through the docs.
So like the docs i'm making consumers.py

def ws_message(message):
    message.reply_channel.send({
        "text": message.content['text'],
})

and routing.py as

from channels.routing import route
from my_proj.consumers import ws_message

channel_routing = [
    route("websocket.receive", ws_message),
]

In my settings file I added channel_layers

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

So when I runserver and in chrome console send the following

socket = new WebSocket("ws://" + 192.168.4.177:8000");
socket.onmessage = function(e) {
    alert(e.data);
}
socket.onopen = function() {
    socket.send("something");
}

I can see in manage.py console that Websocket connect worked and the connection is established, but the receive part is not handled and is not seen in console so alert from js code is not raised. So what am I doing wrong?

Upvotes: 2

Views: 1880

Answers (1)

arminrock
arminrock

Reputation: 535

The problem was with the version of Twisted. By now the latest version of it is 16.3.0 but the Channels require 16.2.0 version. So with 16.2.0 version of Twisted it works as should.

Upvotes: 6

Related Questions