# Subscriptions — GraphQL

Source: https://www.skillbyai.com/en/graphql/m-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.

```graphql
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.

**Quiz:** Which transport is commonly used for GraphQL subscriptions?

- [ ] FTP
- [x] WebSockets
- [ ] SMTP
- [ ] Plain HTTP GET without streaming

*Answer:* WebSockets. Long-lived, two-way connections.
