Wednesday, April 5, 2017

Re: Django Channels - Class Based Consumers

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

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

On Tuesday, 4 April 2017 13:57:07 UTC+1, Andrew Godwin wrote:
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.thom...@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...@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.

--
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/e00a2db6-db82-45e4-a08c-bb805488a37e%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/CAFwN1upW9CYNGkcM-VNzkycEzV%3Dj%3DazNp%2BnBfx6KRMd%3D9heDGA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment