Hi Alex,

Consumers aren't designed to be run outside of a Channels or ASGI
environment. If you read the ASGI spec (
https://github.com/django/asgiref/blob/master/specs/asgi.rst), you'll see
that to run a consumer - which is an ASGI app - you need to do quite a bit
more than you did there, in particular making an event loop and running its
coroutine.

Instead, if you want to receive on a channel layer channel externally, I
recommend calling channel_layer.receive directly. It takes a channel name
to receive on and blocks until there's a message. Ideally you should do
this all within an async context rather than using async_to_sync, but I
believe both should work.

Andrew

On Tue, Mar 13, 2018 at 12:15 AM, Alex <alexanderhalf...@gmail.com> wrote:

> Hi,
>
>
> The docs explain that it is possible to publish to the channel layer from
> outside of a consumer: https://channels.readthedocs.io/en/latest/
> topics/channel_layers.html#using-outside-of-consumers I need to do the
> opposite. I have a fairly complex python script that reads live data from
> pubnub, processes it, and pushes it to consumers via groups on the
> channel_layer. This works fine, but I need consumers to be able to announce
> their presence to this script so that it can push them data (it currently
> pushes to the channel layer only when it gets new data from pubnub, which
> could be every 24 hours).
>
>
> I've decided to solve this by having the consumers publish to a 'presence'
> channel on connect. I now need the pubnub source script to listen to this
> channel. I've tried adding the below to the script, and it no longer throws
> errors, but it doesn't actually respond to messages. It successfully joins
> the channel layer, but the message handler 'receive_json', never fires.
>
>
> This is the class, and then the initiation further down in the code:
> from channels.generic.websocket import JsonWebsocketConsumer
>
>
> class channelConsumer(JsonWebsocketConsumer):
>
>     def __init__(self):
>         self.channel_name = 'source'
>         def join(self):
>         async_to_sync(channel_layer.group_add)('presence', self.
> channel_name)
>     def receive_json(self, message):
>         print("Presence Detected")
>         # do some stuff
>
> global channel_layer
> channel_layer = get_channel_layer()
>
> global listener
> listener = channelConsumer()
> listener.join()
>
>
>
>
> --
> 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/08086bed-67a8-4fd6-8eec-0c417423ea8b%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/08086bed-67a8-4fd6-8eec-0c417423ea8b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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/CAFwN1ur6Dxa%3D6Dfw2PVBKURa55xPxrDRgPa7i6SkKsDbHdrXqw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to