<commit_before>---
title: Subscriptions
subtitle: Adding subscriptions to Apollo Server
---

Subscriptions are GraphQL operations that watch events emitted from Apollo Server.
The native Apollo Server supports GraphQL subscriptions without additional configuration.
All integration that allow HTTP servers, such as express and Hapi, also provide GraphQL subscriptions.

## Subscriptions Example

Subscriptions depend on use a publish and subscribe primitive to generate the events that notify a subscription. `PubSub` is a factory that creates event generators that is provided by all supported packages. `PubSub` is an implementation of the `PubSubEngine` interface, which has been adopted by a variety of additional [event-generating backends](#PubSub-Implementations).

```js
const { PubSub } = require('apollo-server');

const pubsub = new PubSub();
```

Subscriptions are another root level type, similar to Query and Mutation. To start, we need add to add the `Subscription` type to our schema:

```js line=2-4
const typeDefs = gql`
type Subscription {
  postAdded: Post
}
type Query {
  posts: [Post]
}
type Mutation {
  addPost(author: String, comment: String): Post
}
type Post {
  author: String
  comment: String
}
`<commit_msg>Fixed a couple typos (#1309)<commit_after>---
title: Subscriptions
subtitle: Adding subscriptions to Apollo Server
---

Subscriptions are GraphQL operations that watch events emitted from Apollo Server.
The native Apollo Server supports GraphQL subscriptions without additional configuration.
All integrations that allow HTTP servers, such as express and Hapi, also provide GraphQL subscriptions.

## Subscriptions Example

Subscriptions depend on use of a publish and subscribe primitive to generate the events that notify a subscription. `PubSub` is a factory that creates event generators that is provided by all supported packages. `PubSub` is an implementation of the `PubSubEngine` interface, which has been adopted by a variety of additional [event-generating backends](#PubSub-Implementations).

```js
const { PubSub } = require('apollo-server');

const pubsub = new PubSub();
```

Subscriptions are another root level type, similar to Query and Mutation. To start, we need add to add the `Subscription` type to our schema:

```js line=2-4
const typeDefs = gql`
type Subscription {
  postAdded: Post
}
type Query {
  posts: [Post]
}
type Mutation {
  addPost(author: String, comment: String): Post
}
type Post {
  author: String
  comment: String
}
`
{'fraction_non_alphanumeric': 0.07168602322787344, 'fraction_numerical': 0.0032038446135362435, 'mean_word_length': 4.976076555023924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1196977', 'n_tokens_mistral': 662, 'n_tokens_neox': 633, 'n_words': 317}