Django Channels Model Binding not sending messages

134 views
Skip to first unread message

Zac Kwan

unread,
Dec 13, 2017, 7:33:07 AM12/13/17
to Django users
In production, I have setup asgi, wsgi and nginx. My javascript frontend manage connected successfully base on the logs in runworker. However, when a new model is created (DRF API, Admin or Shell), it does not send a message to the group where my frontend is connected. It work perfectly in runserver.

I had did some testing in the python manage.py shell and found out that it work when I try to send a group message myself first.

$ python manage.py shell


Python 3.6.2 (default, Aug 27 2017, 18:32:19)


[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin


Type "help", "copyright", "credits" or "license" for more information.


(InteractiveConsole)


>>> from channels import Group
>>> from github_hook.models import Feed
>>>
>>> Feed.objects.create(name="foo") # Model is created but frontend did not receive any message
>>>
>>> Group("news").send({"text": "dsa"}) # After I do this, a print I had in consumers.py happened
>>> consumers.py is run!
>>>
>>>  Feed.objects.create(name="foo") # Now when i created a new object, frontend successfully received the mssage


The following is my CHANNEL_LAYERS setup:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        },
        "ROUTING": "github_hook.routing.channel_routing",
    },
}


I had been on this for 2 weeks, still caa't figure out what is wrong. Can anyone help me figure out whats wrong? 

It seems to me that my application that is served by WSGI, when created a model via admin, it does not trigger the message sending. It seems to be missing some init on consumers.py? 

Andrew Godwin

unread,
Dec 13, 2017, 1:45:49 PM12/13/17
to django...@googlegroups.com
That is odd behaviour - have you tried to see if it's actually the group send, or something like trying to send it twice? Can you post your binding configuration?

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/eafc6aa0-d620-4e2a-af22-a715845fb463%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zac Kwan

unread,
Dec 13, 2017, 8:03:05 PM12/13/17
to Django users
I have a console.log in my javascript that verified is the newly created Feed that got sent. This is my binding, consumers and routing class:

#bindings.py
class FeedBinding(WebsocketBinding):

    model = Feed
    stream = "feed"
    fields = ["__all__"]

    @classmethod
    def group_names(cls, instance):
        return ["feed-grp", ]

    def has_permission(self, user, action, pk):
                return True

# consumers.py
class Demultiplexer(WebsocketDemultiplexer):

        print("consumers.py is run")
    consumers = {
        "feed": FeedBinding.consumer,
    }

        def connection_groups(self):
        return ["feed-grp", ]

# routing.py
channel_routing = [
    route_class(Demultiplexer, path="^/binding/"),
]


I still couldn't get the sending to work via admin or DRF API call when creating a `Feed` object. This somehow only happens in the production setup, runserver works fine.

Really appreciate your help, thanks for the great library!

Cheers
Zac
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Zac Kwan

unread,
Dec 18, 2017, 10:46:28 AM12/18/17
to Django users
For now, I manage to get it to work in production if I added the following to my `apps.py`.

#apps.py

from django.apps import AppConfig


class HookConfig(AppConfig):
    name = 'hook_app'

    def ready(self):
        import hook_app.signals
        # only need to do this for production so that when binding model is created it send out to group
        from channels import Group
        Group("Test")

It seems to be some init for channels is missing. After I added the above, creating my models via admin or DRF works, it sends out the message to the correct group defined in my consumers.py. 

Although it works for my production server now, I am still unsure why it happens only in my production environment but not runserver. If anyone knows why, please reply. Thanks!
Reply all
Reply to author
Forward
0 new messages