पाठ 12 / 25
Subscriptions
Real-time updates.
Streams of events
A subscription operation keeps a connection open and pushes results to the client when events occur, such as an order status changing or a chat message arriving. Transports are typically WebSockets (the graphql-ws protocol) or server-sent events. On the server, resolvers subscribe to an event source (a pub/sub system such as Redis or Kafka in multi-instance deployments). Authorise subscriptions at connection time and per event, and consider whether polling or simpler server-sent events would suffice.
Subscribing to order updates
A client subscription.
subscription OrderUpdates($orderId: ID!) {
orderStatusChanged(orderId: $orderId) {
id
status
updatedAt
}
}Use a shared pub/sub with several servers
An in-memory event emitter only reaches clients connected to the same server instance.
त्वरित जाँच: Which transport is commonly used for GraphQL subscriptions?
- FTP
- WebSockets
- SMTP
- Plain HTTP GET without streaming
Answer
WebSockets — Long-lived, two-way connections.