django channels in the shell

91 views
Skip to first unread message

chris rose

unread,
Feb 19, 2017, 10:49:09 AM2/19/17
to Django users
I'm trying to implement a custom management command into my app.

its purpose would be to check the database for a poll which fits time parameters and update clients via channels.

I want to get a list of the clients in a group, then decide whether or not to send them new information based on their session data

I have been exploring this in the shell and im wondering why my client doesn't receive a message when i use the following code:

from django.conf import settings
from django.utils.module_loading import import_string

config = getattr(settings, "CHANNEL_LAYERS", {})
backend_class = import_string(config['default']['BACKEND'])
channel_layer = backend_class(config['default'].get("CONFIG", {}))

channel = channel_layer.group_channels('poll')[0]
channel_layer.send(channel, {"text": "hello world!"})


chris rose

unread,
Feb 22, 2017, 11:19:28 PM2/22/17
to Django users
solved it.. I had to explicitly add the hosts parameter

from django.conf import settings
from django.utils.module_loading import import_string

config = getattr(settings, "CHANNEL_LAYERS", {})
hosts = config['default']['CONFIG']['hosts']
backend_class = import_string(config['default']['BACKEND'])
channel_layer = backend_class(hosts=hosts)

Andrew Godwin

unread,
Feb 22, 2017, 11:25:03 PM2/22/17
to django...@googlegroups.com
I would recommend using the channel layer object directly instead in future:

from channels.asgi import get_channel_layer
channel_layer = get_channel_layer()

That will make sure all settings are applied the same.

Andrew

--
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...@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/b4e8cb61-4428-48d6-aeb9-463e21398dfe%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

chris rose

unread,
Feb 23, 2017, 3:32:37 AM2/23/17
to Django users
thank you.

i completely overlooked that function
Reply all
Reply to author
Forward
0 new messages