esyir esyir - 1 year ago 290
Python Question

Django channels: Echo example issues

I'm following the sample code provided by the channels documentation and have run into a issue. The django server successfully accepts a websocket from the browser and sending appears to work. However server-side processing of the message (ws_message) does not appear to occur, and no reply (nor any alert) is registered browser-side.

Sending seems to work, but no reply

This behavior is highly similar to that observed in [SOLVED]: Django channels - Echo example not working. However while switching to twisted 16.2.0 was the solution to that case, I am already on twisted 16.2.0.

Code snippets are as follows:

from django.http import HttpResponse
from channels.handler import AsgiHandler

def ws_message(message):
print("sending message ", message.content["text"])
"text": message.content["text"]

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

channel_routing = [
route("websocket.recieve", ws_message),


"BACKEND": "asgiref.inmemory.ChannelLayer",
"ROUTING": "argent_display.routing.channel_routing"

The django dev server is then run ( runserver) and the following executed via the browser console:

socket = new WebSocket("ws://" + + "/chat/");
socket.onmessage = function(e) {
socket.onopen = function() {
socket.send("hello world");

Upon receiving a message, an alert should be given and the console logged to. However, neither occurs.

Answer Source

You should connect ws_message consumer to websocket.receive not websocket.recieve.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download