Skip to content

Commit

Permalink
Introduce initial payload.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Zelenchuk committed Nov 9, 2023
1 parent 09a2ffd commit 2909d63
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions channels_graphql_ws/graphql_ws_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ async def on_operation(self, op_id, payload):

# ------------------------------------------------------------------- IMPLEMENTATION

SKIP = object()

# A prefix of Channel groups with subscription notifications.
group_name_prefix: str = "GQLWS"

Expand Down Expand Up @@ -914,6 +916,10 @@ async def _on_gql_start__initialize_subscription_stream(
# `notification_queue_lock` as a guard while reading or writing
# to the queue.
notification_queue: asyncio.Queue = asyncio.Queue(maxsize=queue_size)
# Enqueue the initial payload.
initial_payload = subscription_class.initial_payload
if initial_payload is not self.SKIP:
notification_queue.put_nowait(Serializer.serialize(initial_payload))
# Lock to ensure that `notification_queue` operations are
# thread safe.
notification_queue_lock = threading.RLock()
Expand Down
5 changes: 5 additions & 0 deletions channels_graphql_ws/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ class Subscription(graphene.ObjectType):
# Useful to skip intermediate notifications, e.g. progress reports.
notification_queue_limit: Optional[int] = None

# Initial payload. Set it to send an "initial" response with this
# payload to a client as soon as it is subscribed (before any call
# to `Subscription.broadcast`).
initial_payload = GraphqlWsConsumer.SKIP

@classmethod
def broadcast(cls, *, group=None, payload=None):
"""Call this method to notify all subscriptions in the group.
Expand Down

0 comments on commit 2909d63

Please sign in to comment.