question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

group_send does not work for a model instance

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
sevdogcommented, 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 <model>”, if you do not see any I suppose that the serializer you ara using is failing silently.

0reactions
carltongibsoncommented, Jan 24, 2021

What @sevdog says is correct.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cant send a message to a Django channel via group_send
Let me try to clear it up. When a client connects to the Channels server, a consumer instance or channel is created for...
Read more >
Why doesn't my isinstance() work?
Hola, I'm having trouble getting a subclass to trigger an isinstance(). Code below: model.py excerpt: ------------ from django.db import models
Read more >
Channel Layers — Channels 4.0.0 documentation
Channel layers allow you to talk between different instances of an application. They're a useful part of making a distributed realtime application if...
Read more >
How Terrorist Groups End: Lessons for Countering al Qa'ida
2. Terrorism—Prevention—International cooperation. 3. Intelligence service. 4. Problem-oriented policing. 5. Qaida (Organization). I ...
Read more >
Model instance reference - Django documentation
Issues an SQL DELETE for the object. This only deletes the object in the database; the Python instance will still exist and will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found