GraphQL Subscriptions: Transports, Security, and Scaling

Choose GraphQL subscription transports, plan authorization and reconnects, and scale event delivery across servers with security and deployment checks.

published: reading time: 4 min read author: GeekWorkBench
Quick Summary

Choose GraphQL subscription transports, plan authorization and reconnects, and scale event delivery across servers with security and deployment checks.

GraphQL Subscriptions: Transports, Security, and Scaling

GraphQL defines a subscription operation for receiving updates over time. It does not require a particular transport. An implementation may use WebSockets, server-sent events (SSE), or another approach, so clients and servers need to agree on the protocol they support.

The schema is the easy part. Choosing a transport and deciding how authorization, delivery, and recovery work takes more planning. For the wider API trade-offs, see GraphQL vs REST.

The Operation and the Transport Are Separate

A subscription is declared in the schema and sent as a GraphQL operation:

type Subscription {
  orderStatus(orderId: ID!): OrderStatus!
}

subscription TrackOrder($orderId: ID!) {
  orderStatus(orderId: $orderId) {
    status
    updatedAt
  }
}

The operation describes which event data the client wants. The chosen transport carries the long-lived request and its responses. The GraphQL subscriptions guide notes that GraphQL does not select that transport for an implementation.

WebSockets provide a persistent, bidirectional connection, as described in RFC 6455. SSE delivers server events over HTTP for one-way updates from server to client; the HTML Standard defines the event stream. Neither choice is universal. Check client library support, authentication flow, reverse proxies, load balancers, connection limits, and how the service will deploy before choosing. Polling can be simpler when updates are infrequent; a webhook is usually a server-to-server notification pattern.

Plan the Event Path

Most subscription systems connect a GraphQL resolver to a publish/subscribe source. A mutation or another service publishes an event, and the resolver filters or shapes that event for subscribed clients. In a multi-server deployment, use a shared event source or broker when a client may be connected to a different server from the publisher.

Order service -> event broker -> subscription resolver -> connected client

The broker does not decide whether every connected client should see an event. Check the tenant, order, or other resource permissions before delivering it. If access can change during a long-lived connection, define when to reauthorize or close the subscription. Keep event payloads small and limit active subscriptions, message rates, and expensive selection sets so one client cannot hold disproportionate resources.

Reconnects, Missed Events, and Cleanup

Connections can close during deploys, network changes, or proxy timeouts. Choose a reconnect policy with backoff, remove server-side listeners when a subscription ends, and decide whether the client resumes from a cursor or refetches current state. Without a resume mechanism, events sent while the client was offline may be missed; document that behavior instead of implying delivery is guaranteed.

Also consider the race between an initial query and subscription setup. A client may fetch a snapshot, then subscribe after an update has already occurred. A cursor, version field, or follow-up refetch can close that gap, depending on the event source and product requirements.

Deployment Checklist

  • Confirm that the client and server use the same supported subscription protocol.
  • Test authentication at connection time and authorization for each event or resource.
  • Verify proxy timeouts, connection limits, and graceful shutdown behavior.
  • Use a shared event source when publishers and subscribed clients can reach different server instances.
  • Define reconnect, missed-event, cleanup, and resynchronization behavior.
  • Bound active connections, event payload size, and per-operation cost.
  • Prefer polling or another simpler mechanism if clients do not need frequent incremental updates.

For identity and access rules around GraphQL operations, see API authentication vs authorization and the OWASP GraphQL Cheat Sheet.

Conclusion

Treat the subscription operation as the data contract and choose its transport to fit your clients and deployment. A production design also needs an event source, authorization over the connection lifetime, resource limits, and a clear recovery path when clients disconnect.

Category

Related Posts

GraphQL Caching: HTTP, Persisted Queries, and Clients

Compare HTTP, persisted-query, server, and normalized client caching for GraphQL, including cache keys, authorization boundaries, freshness, and invalidation.

#graphql #caching #persisted-queries

GraphQL Federation: Subgraphs, Routing, and Trade-offs

Learn how GraphQL federation composes subgraphs, routes operations, and affects service ownership, query planning, latency, failure handling, and authorization.

#graphql #federation #microservices

CSRF, CORS, and SSRF: Defending Web Request Boundaries

Learn how CSRF, CORS, and SSRF differ, then apply cookie, origin, allowlist, and egress controls to protect browser and server request boundaries.

#web-security #csrf #cors