0
0
GraphQLquery~10 mins

Subscription syntax in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subscription syntax
Client sends subscription request
Server registers subscription
Event occurs on server
Server sends data update to client
Client receives real-time data
Repeat on new events
The client subscribes to a data stream, the server listens for events, and sends updates to the client in real-time.
Execution Sample
GraphQL
subscription OnMessageAdded {
  messageAdded {
    id
    content
    author
  }
}
This subscription listens for new messages and receives their id, content, and author when added.
Execution Table
StepActionServer StateClient StateData Sent
1Client sends subscription request OnMessageAddedSubscription registered for messageAddedWaiting for dataNone
2New message event occurs on serverDetects new message eventWaiting for dataNone
3Server sends messageAdded data to clientSubscription activeReceives message with id, content, author{"id": 1, "content": "Hello", "author": "Alice"}
4Client processes received dataSubscription activeData displayed to userDisplayed message data
5Another message event occursDetects new message eventWaiting for dataNone
6Server sends new messageAdded dataSubscription activeReceives new message data{"id": 2, "content": "Hi!", "author": "Bob"}
7Client processes new dataSubscription activeData updated on screenDisplayed new message data
8Client unsubscribes or connection closesSubscription removedNo longer receiving dataNone
💡 Subscription ends when client unsubscribes or connection closes
Variable Tracker
VariableStartAfter Step 3After Step 6Final
subscriptionStatusNot registeredActiveActiveRemoved
receivedDataNone{"id":1, "content":"Hello", "author":"Alice"}{"id":2, "content":"Hi!", "author":"Bob"}None
clientDisplayEmptyShows first messageShows updated messagesCleared or static
Key Moments - 3 Insights
Why does the client keep receiving data after the initial subscription request?
Because the subscription stays active on the server, sending data whenever the event occurs, as shown in execution_table steps 3 and 6.
What happens if the client closes the connection?
The server removes the subscription and stops sending data, as shown in execution_table step 8.
Does the client need to request data repeatedly?
No, the server pushes updates automatically after the subscription is registered, shown by the continuous data sent in steps 3 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the subscriptionStatus after step 3?
ANot registered
BRemoved
CActive
DPending
💡 Hint
Check the variable_tracker row for subscriptionStatus at After Step 3
At which step does the client first receive data?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at execution_table Data Sent column to see when data is first sent
If the client unsubscribes earlier, which step would be skipped?
AStep 3
BStep 6
CStep 8
DStep 1
💡 Hint
Unsubscribing stops further data, so no new data after first send (step 6)
Concept Snapshot
GraphQL subscriptions let clients listen for real-time data.
Syntax starts with 'subscription' keyword.
Server pushes updates when events happen.
Client receives data automatically until unsubscribed.
Useful for live updates like chat messages or notifications.
Full Transcript
GraphQL subscriptions allow a client to open a real-time connection to the server. The client sends a subscription request specifying the event to listen for. The server registers this subscription and waits for relevant events. When an event occurs, the server sends the updated data to the client immediately. The client receives and processes this data, updating the display or state. This cycle repeats for each new event until the client unsubscribes or closes the connection. This mechanism enables live data updates without repeated requests.