Wednesday, August 30, 2017

get json sent to channels in view

I have this javascript code that send data to channels

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

I'm curios how from message I can get the json {"test":"data"}

here's the view

# Connected to websocket.connect  @channel_session  def ws_connect(message, key):      # Accept connection      message.reply_channel.send({"accept": True})

​if i try message.content['test'] I get a key error

​    key = message['test']
  File "/usr/local/lib/python2.7/dist-packages/channels/", line 36, in __getitem__
    return self.content[key]
KeyError: 'test'

Best Regards,
Samuel Muiruri.
Web Designer | +254 738 940064

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment