Skip to content

Conversation

@jaydenwindle
Copy link
Owner

@jaydenwindle jaydenwindle commented Feb 14, 2020

This PR fixes #6 by allowing users to manually control which Channel Layers groups a subscribed client is added to, and which groups subscription events are broadcast over.

The consumer instance is now passed to each subscription resolver as a the root value, and exposes a function called subscribe. When called, this function will add the currently connected client to the specified group and return an Observable of all values sent to that group.

A model mixin called SubscriptionModelMixin has been created, which uses django-lifecycle instead of Django signals to automatically trigger subscription events when model instances are created, updated, or deleted.

This PR also removes the legacy SubscriptionEvent and ModelSubscriptionEvent classes, and removes the default post_save_subscription and post_delete_subscription signal handlers, as they are no longer used.

Example:

# your_app/graphql/subscriptions.py
class MessageCreatedSubscription(graphene.ObjectType):
    message_created = graphene.Field(MessageType)
    message_updated = graphene.Field(MessageType, id=graphene.String())

    def resolve_message_created(root, info):
        return root.subscribe("messageCreated")
    
    def resolve_message_created(root, info, id):
        return root.subscribe(f"messageCreated.{id}")

# your_app/models.py
from django.db import models
from graphene_subscriptions.models import SubscriptionModelMixin

class Message(SubscriptionModelMixin, models.Model):
    # ...

This PR also fixes #8 and #2.

@jaydenwindle jaydenwindle mentioned this pull request Feb 14, 2020
subscription = await subscribe(query)

subscription = """
await asyncio.sleep(0.01)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need it? It looks like we didn't need before.

@oldani
Copy link

oldani commented Apr 26, 2020

@jaydenwindle Will this get released any soon? This will come handy in a project I am working on.

@jaydenwindle
Copy link
Owner Author

Hey @oldani! Yes, I'm planning to merge this PR and release v2 as soon as I get a chance to write a v1 -> v2 migration guide for the docs, since there are several breaking changes. I've been a bit busy with other projects recently so haven't had time to finish it.

If you'd like to help with the migration guide I'd definitely accept a PR!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove signals dependency Scalability

4 participants