⬡ GraphQL Beginner

What is a GraphQL subscription?

Answer

A subscription is a real-time operation in GraphQL that maintains a persistent connection to the server and pushes data to the client when specific events occur. Unlike queries and mutations (request-response), subscriptions use WebSockets (or SSE) to stream updates. Example: subscription { messageAdded(channelId: "1") { id text author { name } } } — the client receives a new payload every time a message is added. Subscriptions are defined in the schema: type Subscription { messageAdded(channelId: ID!): Message! }. Server-side, subscriptions are powered by a pub/sub system (Redis, in-memory EventEmitter). Common use cases: chat apps, live dashboards, notifications, collaborative editing, and real-time data feeds.