GraphQL Subscriptions Cheat Sheet

Deliver realtime updates with WebSockets and streaming

Last Updated: November 21, 2025

Subscription Flow

Stage Description
Define schema Add a subscription field with documented payload
Set up resolver Return AsyncIterator (PubSub, Redis, etc.)
Connect clients Use WebSocket + `connection_init` handshake
Broadcast events Publish to topic once data is ready

Patterns & Commands

pubsub.publish('NEW_MESSAGE', payload)
Push event to subscribers
filter: ({ args, context }) => args.roomId === context.roomId
Limit events per subscription
cleanup iterator
Close AsyncIterator on disconnect

Security

Authenticate the WebSocket handshake, authorize each topic, and throttle noisy clients.

💡 Pro Tip: Publish subscription events only after the underlying transactional work commits.
← Back to Databases & APIs | Browse all categories | View all cheat sheets