What is a GraphQL subscription? Streaming events to clients
A GraphQL subscription is a long-running operation that emits results with the same GraphQL selection whenever a server-side event occurs. The server first connects to a source event stream, then executes the operation for each event to produce a response event. The GraphQL specification defines this event-processing model, but not transport, resend, buffering, or delivery guarantees, so design event delivery and connection lifecycle separately.
A GraphQL subscription is a long-running operation that emits results with the same GraphQL selection whenever a server-side event occurs. The server first connects to a source event stream, then executes the operation for each event to produce a response event. The GraphQL specification defines this event-processing model, but not transport, resend, buffering, or delivery guarantees, so design event delivery and connection lifecycle separately.
This guide explains the GraphQL model and an Apollo Server example using WebSockets. Confirm protocol and server support for your implementation; no deployment or recovery behavior is claimed as tested.
How does a subscription flow work?
Define subscribable fields on the schema's Subscription root. A client selects one with a subscription operation. For example, a pub/sub system can publish a reviewCreated event when a review is created; the subscription resolver consumes it, and GraphQL executes the requested subfields to form the result.
type Subscription {
reviewCreated(productId: ID!): ReviewEvent!
}
subscription ProductReviewFeed($productId: ID!) {
reviewCreated(productId: $productId) {
reviewId
productId
rating
}
}This is an illustrative contract. A subscription operation must have exactly one top-level field. A document can define multiple distinct subscription operations, but one operation cannot select multiple root subscription fields.
A resolver commonly has two stages. The subscribe stage creates the event source; each event then becomes the input for executing the subscription selection set. A pub/sub message can be source data for computing the selected GraphQL result rather than the complete result itself. The implementation follows the chosen GraphQL library's API.
A subscription is not a live query
A subscription is a long-lived request driven by server events. A live query is a separate concept in which query results are recomputed as data changes; the GraphQL specification does not define it as the same feature. Decide whether clients need event-driven updates or simply a fresh read. For infrequent changes, polling or refetching after user action may be simpler.
The server chooses transport and protocol
GraphQL does not prescribe a subscription transport. Implementations commonly use WebSockets or Server-Sent Events, and clients must support the same contract as the server. Current Apollo Server docs describe adding the third-party graphql-ws library for subscriptions in non-federated graphs and say the older subscriptions-transport-ws library is no longer actively maintained. This is Apollo-specific, not a guarantee for other servers.
With graphql-ws, the WebSocket connection and subscription operation have separate lifecycles. The client sends connection_init; the server acknowledges it before operations are registered. Event results are associated with an operation ID. When the client completes an operation or the connection closes, clean up the subscription and event consumption. Align protocol identifier, authentication location, keepalive, timeout, and close behavior across clients, servers, and tools.
Design event publishing and authorization
Mutations are not the only possible source of events. Any service action that changes data can publish an event needed by a subscription. An in-memory pub/sub is useful in development, but Apollo warns that its in-memory implementation reaches only one server instance and is not recommended for production. Multi-instance subscribers need a shared broker or another durable event-delivery layer.
Subscription arguments often scope events by product, room, or user. Do not broadcast every event to every subscriber; filter events and check authorization so one user's data is not exposed to another. Authentication at connection time does not replace per-operation authorization and data-visibility checks. Include permission changes and token expiry during long-lived connections in the policy.
Specify recovery and delivery guarantees separately
Subscriptions keep an open connection and operation variables/context. If an instance changes or a connection drops, a client may need to subscribe again. GraphQL does not define transport QoS, acknowledgements, buffering, or resend, so reconnect does not imply gap-free delivery.
If missing events are unacceptable, attach event IDs or cursors and provide a way to fetch the missed range or resynchronize current state after reconnect. Duplicate events can occur, so clients may need idempotent handling or deduplication by ID. Define retention, ordering scope, expired-cursor behavior, and races between an initial query and the subscription. These are application delivery guarantees, not GraphQL guarantees.
When should you use subscriptions?
Consider subscriptions when data changes frequently and incrementally and clients need updates close to real time. If changes are rare or a fresh read is enough, polling, refetching after user actions, or mobile push can be simpler. Subscriptions add operational work for persistent connections, authentication, event filtering, horizontal scaling, resource limits, reconnection, and observability.
Adoption checklist
Is the event source and the subscription field's result defined?
Does each operation select exactly one root subscription field?
Do the server and every client/tool use the same transport and protocol?
Can the production pub/sub system deliver across server instances?
Are per-user authentication, event filtering, and authorization checked?
Are close, reconnect, missed/duplicate events, and cursor expiry handled?
Are limits set for concurrent connections, event fan-out, message size, and request cost?
Sources
[GraphQL subscriptions guide](https://graphql.org/learn/subscriptions/)
[GraphQL specification, September 2026: subscription execution and scaling](https://spec.graphql.org/September2026/)
[Apollo Server subscriptions](https://www.apollographql.com/docs/apollo-server/data/subscriptions)
[graphql-ws WebSocket protocol](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md)
[GraphQL security guide](https://graphql.org/learn/security/)