Tuesday, April 4, 2017

Re: Django Channels - Class Based Consumers

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

On Tue, Apr 4, 2017 at 9:55 AM, Dan Alderman <daniel.thomas.alderman@gmail.com> wrote:
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

--
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-users@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.

--
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-users@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/CAFwN1upFrxe2D%3DCjnUQZV2x6cszbVF9TRcNqMKZsgwMGAHz_xA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment