Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

group_send does not work for a model instance #1594

Closed
nebiyuelias1 opened this issue Dec 15, 2020 · 5 comments
Closed

group_send does not work for a model instance #1594

nebiyuelias1 opened this issue Dec 15, 2020 · 5 comments

Comments

@nebiyuelias1
Copy link

I am trying to use group_send using a model instance:

def group_send(moogt, notification):
    channel_layer = get_channel_layer()
    if moogt:
        async_to_sync(channel_layer.group_send)(f'{moogt.id}', notification)

Here notification, contains a model object like this:

notification = {
        'type': 'receive_group_message',
        'argument': argument,
        'event_type': message_type
    }

Of course, I do serialize the model object in the consumer, inside receive_group_message. But I am not getting anything in my websocket clients.

@pawel-czarnecki
Copy link

pawel-czarnecki commented Dec 17, 2020

Actually I've encountered the same problem, my group doesn't received anything back while using group_send from channel_layer. I haven't seen any changes in docs about this topic. I'm using v3.0.2

@pawel26
Copy link

pawel26 commented Dec 17, 2020

@nebiyuelias1 which version of channels are you using?

@nebiyuelias1
Copy link
Author

channels==2.4.0

@sevdog
Copy link
Contributor

sevdog commented Jan 15, 2021

group_send behaviour dependes on channel layer.

If you use InMemoryChannelLayer it is fine to leave model instances into messages. but if you use RedisChannelLayer (or any other channel layer which needs to interact with an external system) this is not possible anymore since message data needs to be serialized in the process. By default django model instances are not serializable by common used serializers (json/msgpack).

Of course, I do serialize the model object in the consumer, inside receive_group_message.

You also need to serialize the model in your group_send before passing to channel layer.

However I would expect some errors to be thrown like "unable to serialize instance of ", if you do not see any I suppose that the serializer you ara using is failing silently.

@carltongibson
Copy link
Member

What @sevdog says is correct.

I don't think there's enough to really go on as a concrete issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants