Skip to content

Commit e37937e

Browse files
author
Andrey Zelenchuk
committed
Introduce initial payload.
1 parent 9db69f8 commit e37937e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

channels_graphql_ws/graphql_ws_consumer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ async def _register_subscription(
679679
groups,
680680
publish_callback,
681681
unsubscribed_callback,
682+
initial_payload,
682683
notification_queue_limit=None,
683684
):
684685
"""Register a new subscription when client subscribes.
@@ -697,6 +698,7 @@ async def _register_subscription(
697698
subscription groups current subscription belongs to.
698699
unsubscribed_callback: Called to notify when a client
699700
unsubscribes.
701+
initial_payload: Initial payload.
700702
notification_queue_limit: LImit for the subscribtion
701703
notification queue. Default is used if not set.
702704
@@ -720,13 +722,25 @@ async def _register_subscription(
720722
queue_size = self.subscription_notification_queue_limit
721723
notification_queue = asyncio.Queue(maxsize=queue_size)
722724

725+
# Enqueue the initial payload.
726+
if initial_payload is not self.SKIP:
727+
notification_queue.put_nowait(Serializer.serialize(initial_payload))
728+
723729
# Start an endless task which listens the `notification_queue`
724730
# and invokes subscription "resolver" on new notifications.
725731
async def notifier():
726732
"""Watch the notification queue and notify clients."""
727733

728734
# Assert we run in a proper thread.
729735
self._assert_thread()
736+
737+
# Dirty hack to partially workaround the race between:
738+
# 1) call to `result.subscribe` in `_on_gql_start`; and
739+
# 2) call to `trigger.on_next` below in this function.
740+
# The first call must be earlier. Otherwise, first one or more notifications
741+
# may be lost.
742+
await asyncio.sleep(1)
743+
730744
while True:
731745
serialized_payload = await notification_queue.get()
732746

channels_graphql_ws/subscription.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ class Subscription(graphene.ObjectType):
148148
# Return this from the `publish` to suppress the notification.
149149
SKIP = GraphqlWsConsumer.SKIP
150150

151+
# Initial payload. Set it to send an "initial" response with this
152+
# payload to a client as soon as it is subscribed (before any call
153+
# to `Subscription.broadcast`).
154+
initial_payload = GraphqlWsConsumer.SKIP
155+
151156
# Subscription notifications queue limit. Set this to control the
152157
# amount of notifications server keeps in queue when notifications
153158
# come faster than server processing them. Set this limit to 1 drops
@@ -435,6 +440,7 @@ def unsubscribed_callback():
435440
groups,
436441
publish_callback,
437442
unsubscribed_callback,
443+
cls.initial_payload,
438444
cls.notification_queue_limit,
439445
)
440446

0 commit comments

Comments
 (0)