Django Channels client not consuming

50 views
Skip to first unread message

LEEPS Lab

unread,
May 15, 2018, 4:01:59 PM5/15/18
to Django users
We have an issue implementing django channels in an oTree project (http://otree.readthedocs.io/en/latest/)

Currently we are forced to use django channels version 0.17.3 due to the restriction inside of oTree, so it is possible that we are dealing with version issues.

However, we have an issue implementing a message.reply_channel response where we are successfully adding messages to the reply channel queue, but our client is not consuming these messages to the point of a 

asgiref.inmemory.ChannelLayer.ChannelFull:

Our client code creates the usual websocket consuming scheme

window.onload = function () {
            var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
            var socket = new WebSocket(ws_scheme + '://' + window.location.host + "/hft/{{group.id}}/{{player.id}}/");
            console.log(ws_scheme + '://' + window.location.host + "/hft/{{group.id}}/{{player.id}}/");
            
            // Handle any errors that occur.
            socket.onerror = function (error) {
                console.log('WebSocket Error: ' + error);
            };

            // Show a connected message when the WebSocket is opened.
            socket.onopen = function (event) {
                console.log('connected to oTree');
            };
            // Handle messages sent by the server.
            socket.onmessage = function (event) {
                // var obj = jQuery.parseJSON(event.data);
                // role = obj.state;
                console.log("Received a message");
            };
            // Show a disconnected message when the WebSocket is closed.
            socket.onclose = function (event) {
                console.log('disconnected from oTree');
            };


currently the socket.onmessage function is never called.

Our consumer code looks like this:

class SubjectConsumer(JsonWebsocketConsumer):

    def raw_connect(self, message, group_id, player_id):
        Group(group_id).add(message.reply_channel)    
        log.info('Player %s is connected to Group %s.' % (player_id, group_id))
        self.connect(message, player_id)

    def connect(self, message, player_id):
        player = Player.objects.get(id=player_id)
        player.channel = message.reply_channel
        player.save()

    def raw_receive(self, message, group_id, player_id):
        msg = json.loads(message.content['text'])
        player = Player.objects.get(id=player_id)
        player.receive_from_client(msg)

    def raw_disconnect(self, message, group_id, player_id):
        log = 'Player %s  disconnected from Group %s.' % (player_id, group_id)
        logging.info(log)
        Group(group_id).discard(message.reply_channel)

    def send(self, msg, chnl):
        Channel(chnl).send(msg)

    def broadcast(self, msg, chnl):
        Group(chnl).send(msg)

Usually we will save the reply_channel after connecting with the consumer to a player object that will send a message when finished processing received data from the client. We have already checked that the channels names are correct, and that no errors are thrown in relation to the type of our data and how we're sending it. 

We believe our routing is correct since we can receive data from the client, but here is what we have

channel_routing += [
    route_class(SubjectConsumer, path=r"^/hft/(?P<group_id>\w+)/(?P<player_id>\w+)/"),
    route_class(InvestorConsumer, path=r"^/hft_investor/(?P<group_id>\w+)/"),
    route_class(JumpConsumer, path=r"^/hft_jump/(?P<group_id>\w+)/")
]

We get the error after we successfully send several messages from the consumer's send function but do not receive any on our client websockets. We assume that they are sending correctly because we continue execution after SubjectConsumer.send() and the queue eventually fills.

2018-05-15 10:43:06,813 - ERROR - worker - Error processing message with consumer oTree_HFT_CDA.consumers.SubjectConsumer:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/channels/worker.py", line 120, in run
    consumer(message, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/channels/generic/base.py", line 31, in __init__
    self.dispatch(message, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/channels/generic/base.py", line 69, in dispatch
    return self.get_handler(message, **kwargs)(message, **kwargs)
  File "/home/leeps/oTree_HFT_CDA/oTree_HFT_CDA/consumers.py", line 29, in raw_receive
    Channel(str(message.reply_channel)).send({'a':'b'})
  File "/usr/local/lib/python3.5/dist-packages/channels/channel.py", line 38, in send
    self.channel_layer.send(self.name, content)
  File "/usr/local/lib/python3.5/dist-packages/asgiref/inmemory.py", line 50, in send
    raise self.ChannelFull(channel)
asgiref.inmemory.ChannelLayer.ChannelFull: websocket.send!haCXgRqE
websocket.send!haCXgRqE
2018-05-15 10:43:07,019 - ERROR - worker - Error processing message with consumer oTree_HFT_CDA.consumers.SubjectConsumer:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/channels/worker.py", line 120, in run
    consumer(message, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/channels/generic/base.py", line 31, in __init__
    self.dispatch(message, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/channels/generic/base.py", line 69, in dispatch
    return self.get_handler(message, **kwargs)(message, **kwargs)
  File "/home/leeps/oTree_HFT_CDA/oTree_HFT_CDA/consumers.py", line 29, in raw_receive
    Channel(str(message.reply_channel)).send({'a':'b'})
  File "/usr/local/lib/python3.5/dist-packages/channels/channel.py", line 38, in send
    self.channel_layer.send(self.name, content)
  File "/usr/local/lib/python3.5/dist-packages/asgiref/inmemory.py", line 50, in send
    raise self.ChannelFull(channel)
asgiref.inmemory.ChannelLayer.ChannelFull: websocket.send!haCXgRqE



Package versions: 
daphne                        0.14.3
asgiref                       0.14.0 

Andrew Godwin

unread,
May 15, 2018, 8:23:40 PM5/15/18
to django...@googlegroups.com
Hi,

I'm afraid you're using incredibly old versions of Channels and the bugs you're encountering are almost certainly fixed in more recent releases. I'm not sure there's much I can do.

Andrew

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/32412a05-9503-46ac-b59b-51cb6ee2b3c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages