0
0
GraphQLquery~10 mins

Subscription lifecycle in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subscription lifecycle
Client subscribes
Server registers subscription
Server listens for events
Event occurs
Server sends data update
Client receives update
Client may unsubscribe
Server cleans up subscription
This flow shows how a client starts a subscription, the server listens and sends updates, and how the subscription ends.
Execution Sample
GraphQL
subscription {
  newMessage {
    id
    content
  }
}
This subscription listens for new messages and receives their id and content when they arrive.
Execution Table
StepActionServer StateClient StateData SentNotes
1Client sends subscription requestRegisters subscription for newMessageWaiting for dataNoneSubscription starts
2Server listens for newMessage eventsListening activeWaiting for dataNoneReady to send updates
3New message event occursEvent detectedWaiting for dataNoneEvent triggers data send
4Server sends newMessage dataSends {id:1, content:'Hi'}Receives data{id:1, content:'Hi'}Client gets update
5Client processes dataListening activeData received{id:1, content:'Hi'}Client updates UI
6Client unsubscribesRemoves subscriptionSubscription endedNoneSubscription ends
7Server cleans up resourcesNo active subscriptionSubscription endedNoneCleanup done
💡 Subscription ends when client unsubscribes and server cleans up
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 6Final
subscriptionActivefalsetruetruefalsefalse
dataReceivednonenone{id:1, content:'Hi'}{id:1, content:'Hi'}{id:1, content:'Hi'}
Key Moments - 3 Insights
Why does the server keep the subscription active after sending data?
Because subscriptions stay open to send future updates until the client unsubscribes, as shown in steps 4 and 5.
What happens if the client never unsubscribes?
The server keeps the subscription active and continues sending updates when events occur, as seen in steps 2 to 5.
When does the server clean up the subscription?
Only after the client unsubscribes, as shown in steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the client first receive data?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Check the 'Data Sent' column in the execution table rows
According to the variable tracker, when does 'subscriptionActive' become false?
AAfter Step 1
BAfter Step 6
CAfter Step 4
DAt Start
💡 Hint
Look at the 'subscriptionActive' row values in variable_tracker
If the client never unsubscribes, what would happen to the server state?
AServer keeps listening and sending updates
BServer removes subscription immediately
CServer sends data once then stops
DServer crashes
💡 Hint
Refer to key_moments about subscription lifecycle and server behavior
Concept Snapshot
GraphQL Subscription Lifecycle:
- Client sends subscription request
- Server registers and listens for events
- Server sends data updates on events
- Client receives and processes updates
- Subscription stays open until client unsubscribes
- Server cleans up after unsubscribe
Full Transcript
This visual execution shows the lifecycle of a GraphQL subscription. First, the client sends a subscription request. The server registers this subscription and listens for relevant events. When an event occurs, the server sends data to the client. The client receives and processes this data. The subscription remains active, allowing multiple updates. When the client decides to unsubscribe, the server removes the subscription and cleans up resources. Variables like subscriptionActive track whether the subscription is open. This lifecycle ensures real-time data flow until the client ends it.