GraphQL Subscription
ELI5 — The Vibe Check
GraphQL subscriptions are real-time updates pushed from the server to the client. Instead of polling 'any new messages?' every second, the server tells you 'hey, new message!' the moment it happens. It uses WebSockets under the hood and is perfect for chat, notifications, and live dashboards.
Real Talk
GraphQL subscriptions enable real-time, server-pushed data updates to clients over persistent connections (typically WebSockets). Clients subscribe to specific events, and the server pushes matching data when those events occur. Subscriptions use the same type system as queries and mutations, providing type-safe real-time communication.
Show Me The Code
// Schema
type Subscription {
messageSent(chatId: ID!): Message!
}
// Client
const subscription = gql`
subscription OnMessage($chatId: ID!) {
messageSent(chatId: $chatId) {
id
text
sender { name }
}
}
`;
When You'll Hear This
"Use GraphQL subscriptions for the real-time chat instead of polling." / "Subscriptions push new order updates to the dashboard as they happen."
Related Terms
GraphQL
GraphQL is like ordering food where YOU specify exactly what you want on your plate.
Server-Sent Events
Server-Sent Events (SSE) is like subscribing to a news feed from the server.
WebSocket
WebSocket is like upgrading a walkie-talkie from push-to-talk to a full phone call.