Subscriptions & Real-Time Data
You'll learn to
- -Explain a subscription as a query that stays open and keeps pushing results
- -Filter events on the server for each subscriber, and count the messages that saves
- -Pair a subscription with an initial query, and name what it costs to hold many open connections
Queries and mutations are both request-response: the client asks, the server answers once. A subscription is GraphQL's third kind of operation, for when the client wants to be told about changes as they happen, instead of asking again and again. Typical uses are new messages in a chat, a live order status or a dashboard that updates by itself.
An Analogy: The Newspaper Subscription
Walking to the shop every hour to see whether there is a new edition is polling. A subscription is having the paper delivered when it is printed. And a good delivery service sends you only the sections you ordered, not the whole warehouse for you to sort through at home.
Defining a Subscription
type Subscription {
orderStatusChanged(orderId: ID!): Order!
}
subscription {
orderStatusChanged(orderId: "501") {
id
status
updatedAt
}
}A subscription is defined like a query field, but instead of answering once, it stays open, usually over a WebSocket (a long-lived two-way connection between browser and server). Each time the underlying event fires, a new payload is pushed down it. Here that happens every time order 501's status actually changes, not on a polling timer.
Filtering: Only Send What This Subscriber Asked For
The naive mistake is to push every event on a shared channel to every subscriber and let each client filter. That wastes bandwidth on data the client will throw away, and on a busy system, with thousands of orders changing per minute, every connection carries irrelevant traffic. Filter on the server, scoped to exactly the arguments this subscriber gave, such as orderId: "501".
The demo has two subscribers, Alice watching order 501 and Bob watching order 777, and four events, one of them for an order nobody watches. It counts the messages pushed with and without server-side filtering. The last lines show the second idea in this chapter.
Subscriptions Are Not a Replacement for Queries
A subscription usually pushes the changes, not the full picture a client needs on first load. A well-designed real-time feature pairs an initial query, to fetch the current state when the client first connects, with a subscription that delivers updates from then on. Expecting the subscription alone to deliver both the starting state and every later change is a design mistake.
A subscription holds an open connection per client, which costs very differently from a stateless query. At real scale it needs its own plan for managing connections and scaling out, such as a pub/sub backend (a message hub that fans events out to many server instances), and not just "add a WebSocket handler" to the existing code.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Design Real-Time Feed in the API Design Lab's GraphQL Mastery act.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!