Sending messages to django channel group outside of consumers

1,559 views
Skip to first unread message

Gene

unread,
Jan 11, 2017, 6:03:11 PM1/11/17
to Django users
I'm trying to send messages to channels outside of consumers, as said in documentation but can't get a result.

For example, 
in consumers.py I have:

def channel_send(channel, data):
    channel.send({
        'text': json.dumps(data)
    })

def group_send(kind, data):
    data = {
        'kind': kind,
        'data': data
    }
    channel_send(Group(kind), data)

def ws_connect(message):
    Group("location_update").add(message.reply_channel)
    channel_send(message.reply_channel, {'accept': True})
    group_send('location_update', {'msg': 'connected'})

def ws_disconnect(message):
    # Remove from reader group on clean disconnect
    Group("location_update").discard(message.reply_channel)

def ws_message(message):
    group_send('location_update', {'msg': 'connected'})


When I send message from browser over websocket, I got a response in console:
{"kind": "location_update", "data": {"msg": "connected"}}

but, for example, when I try to use group_send function inside management command, or if I try to use group_send in python manage.py shell regime from shell - I don't get anything in the browser

channels==1.0.1

What may be the issue?


Andrew Godwin

unread,
Jan 11, 2017, 6:06:15 PM1/11/17
to django...@googlegroups.com
That code looks correct. What channel layer are you using?

--
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/d1d54866-c155-40d7-abef-e5157f51d337%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gene

unread,
Jan 11, 2017, 6:07:51 PM1/11/17
to Django users
I tried first with inmemory, then with asgi_redis.RedisChannelLayer
same result
working in runserver mode

четверг, 12 января 2017 г., 2:06:15 UTC+3 пользователь Andrew Godwin написал:
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andrew Godwin

unread,
Jan 11, 2017, 6:11:16 PM1/11/17
to django...@googlegroups.com
It will only work with a Redis channel layer - in memory does not transport things across processes. As long as everything being used has the same channel layer settings and talks to the same Redis, I don't see why it wouldn't work.

Andrew

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.

Gene

unread,
Jan 11, 2017, 6:14:37 PM1/11/17
to Django users
so, should it theoretically work in combination when runserver is launched and I'm running in python manage.py shell ?

in case I will not be able to find a solution, I'll try to reproduce it on some special small project

четверг, 12 января 2017 г., 2:11:16 UTC+3 пользователь Andrew Godwin написал:

Andrew Godwin

unread,
Jan 11, 2017, 6:22:53 PM1/11/17
to django...@googlegroups.com
Yes, as long as the management command uses the same settings and talks to the same Redis server, it should all intercommunicate. If you can get it reproducible I can definitely help more.

Andrew

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.

Andrew Godwin

unread,
Jan 11, 2017, 6:26:37 PM1/11/17
to django...@googlegroups.com
Oh, disregard that, cross-checking with the django-channels IRC channel I see that it's the new message buffering behaviour going awry. You can track the status of the fix in https://github.com/django/channels/issues/481

Gene

unread,
Jan 11, 2017, 7:04:10 PM1/11/17
to Django users
I managed to reproduce it in small project:

see here test case:
https://github.com/emakarov/chantest

четверг, 12 января 2017 г., 2:26:37 UTC+3 пользователь Andrew Godwin написал:

Gene

unread,
Jan 11, 2017, 7:09:50 PM1/11/17
to Django users
After accepting your patch it started to work! :-)

четверг, 12 января 2017 г., 3:04:10 UTC+3 пользователь Gene написал:

Gene

unread,
Jan 11, 2017, 7:10:22 PM1/11/17
to Django users
waiting for update in pip ..

четверг, 12 января 2017 г., 3:09:50 UTC+3 пользователь Gene написал:

Andrew Godwin

unread,
Jan 11, 2017, 7:18:48 PM1/11/17
to django...@googlegroups.com
Yup, I'm waiting on one more pull request to land and I'll issue a 1.0.2 release to address the issue.

Andrew

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.
Reply all
Reply to author
Forward
0 new messages