Django Channels - Class Based Consumers

268 views
Skip to first unread message

Dan Alderman

unread,
Apr 4, 2017, 7:14:08 AM4/4/17
to Django users
Hi guys,

Been trying to get my head around this for ages now but still no luck, hopefully someone can point me in the right direction.

I'm trying to set up a Demultiplexer with a JsonWebsocketConsumer that sends to a group. (There is only one stream in the Demultiplexer at the moment, but I intend to add more).

The prints in each of the functions work when I interact with my app in the web browser, but I'm having trouble getting a response in the browser via websockets.

If I user multiplexer.send() then it works, but it only goes to a single connection (as you'd expect).

consumers.py

from channels.generic.websockets import JsonWebsocketConsumer, WebsocketDemultiplexer

class DatabaseUpdateConsumer(JsonWebsocketConsumer):
strict_ordering = True

def connection_groups(self, **kwargs):
return ["updatedb_results"]

def connect(self, message, multiplexer, **kwargs):
print "Connected!"
multiplexer.group_send(multiplexer.__class__.__name__, self.connection_groups(), {'text': "Connected!"})

def disconnect(self, message, multiplexer, **kwargs):
print("Stream %s is closed" % multiplexer.stream)

def receive(self, content, multiplexer, **kwargs):
print "Message received by server!"
multiplexer.group_send(multiplexer.__class__.__name__, self.connection_groups(), {'text': "Message received by server!"})

class Demultiplexer(WebsocketDemultiplexer):

consumers = {
"updatedb_results": DatabaseUpdateConsumer,
}


index.html

{% extends "base.html" %}

{% block title %}Test Dashboard{% endblock %}

{% block content %}
<div id="currentCount">{{ job_detail|length }}</div>
<br>
{% for line in job_detail %}
<div id="{{ line.id }}">{{ line.job_number }}</div>
{% endfor %}
{% endblock %}

{% block javascript %}
<script>
const webSocketBridge = new channels.WebSocketBridge();
webSocketBridge.connect("ws:/development01:8000/ws/");
webSocketBridge.listen();
webSocketBridge.demultiplex('updatedb_results', function(data, stream) {
console.log(data);
});
</script>
{% endblock %}


routing.py

from channels import route_class
from dashboard.consumers import Demultiplexer

channel_routing = [
route_class(Demultiplexer, path="^/ws/"),
]

Can anyone tell me where I'm going wrong?

Thanks,

Dan

Andrew Godwin

unread,
Apr 4, 2017, 8:57:07 AM4/4/17
to django...@googlegroups.com
Hi Dan,

An initial glance shows that you might be passing the arguments wrong to Multiplexer.group_send(group_name, stream, payload) - I'm not sure why you are using multiplexer.__class__.__name__ as the group name, but the group name you configured in connection_groups is updatedb_results, and the stream you're listening to in the JS client is also updatedb_results, so you should set both of these to that value.

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+unsubscribe@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/965e2ace-f7af-43d8-bf20-cac1745bb51e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dan Alderman

unread,
Apr 5, 2017, 3:15:45 AM4/5/17
to Django users
Hi Andrew,

Many thanks for your input - that does indeed help.

What was really confusing me was what arguments I actually needed to supply to the JsonWebsocketConsumer.group_send() function.

Not fully understanding classmethods, I was trying to pass the cls argument as well.

I understand what the group_name and payload arguments do - but what s the stream argument doing?

Dan
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andrew Godwin

unread,
Apr 5, 2017, 4:18:58 AM4/5/17
to django...@googlegroups.com
The stream is the name of the stream inside the Multiplexer - it's like a sub-channel inside the WebSocket that is used by either end to distinguish packets from each other (it's why you provide it to the JavaScript binding - it needs to know which stream to listen to)

Andrew

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Dan Alderman

unread,
Apr 5, 2017, 4:47:12 AM4/5/17
to Django users
That makes sense.

I got myself in such a muddle I somehow ended up knowing less about Channels than I did a couple of weeks go.

Thanks very much for the help!
Reply all
Reply to author
Forward
0 new messages