Re: Django Channels - Class Based Consumers

2017-04-05 Thread Dan Alderman
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!

On Wednesday, 5 April 2017 09:18:58 UTC+1, Andrew Godwin wrote:
>
> 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  > 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  
>>> 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 %}
 {{ job_detail|length }}
 
 {% for line in job_detail %}
 {{ line.job_number }}
 {% endfor %}
 {% endblock %}

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

Re: Django Channels - Class Based Consumers

2017-04-05 Thread Andrew Godwin
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.alder...@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 
>> 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 %}
>>> {{ job_detail|length }}
>>> 
>>> {% for line in job_detail %}
>>> {{ line.job_number }}
>>> {% endfor %}
>>> {% endblock %}
>>>
>>> {% block javascript %}
>>> 
>>> const webSocketBridge = new channels.WebSocketBridge();
>>> webSocketBridge.connect("ws:/development01:8000/ws/");
>>> webSocketBridge.listen();
>>> webSocketBridge.demultiplex('updatedb_results', function(data, stream) {
>>> console.log(data);
>>> });
>>> 
>>> {% 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/ms
>>> gid/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+unsubscr...@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 

Re: Django Channels - Class Based Consumers

2017-04-05 Thread Dan Alderman
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  > 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 %}
>> {{ job_detail|length }}
>> 
>> {% for line in job_detail %}
>> {{ line.job_number }}
>> {% endfor %}
>> {% endblock %}
>>
>> {% block javascript %}
>> 
>> const webSocketBridge = new channels.WebSocketBridge();
>> webSocketBridge.connect("ws:/development01:8000/ws/");
>> webSocketBridge.listen();
>> webSocketBridge.demultiplex('updatedb_results', function(data, stream) {
>> console.log(data);
>> });
>> 
>> {% 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+unsubscr...@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.


Re: Django Channels - Class Based Consumers

2017-04-04 Thread Andrew Godwin
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.alder...@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 %}
> {{ job_detail|length }}
> 
> {% for line in job_detail %}
> {{ line.job_number }}
> {% endfor %}
> {% endblock %}
>
> {% block javascript %}
> 
> const webSocketBridge = new channels.WebSocketBridge();
> webSocketBridge.connect("ws:/development01:8000/ws/");
> webSocketBridge.listen();
> webSocketBridge.demultiplex('updatedb_results', function(data, stream) {
> console.log(data);
> });
> 
> {% 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+unsubscr...@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+unsubscr...@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.