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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to