Skip to content

Commit 4ce5821

Browse files
UrigoyaacovCR
authored andcommitted
Subscriptions docs suggestions
1 parent e3c0dd8 commit 4ce5821

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

website/pages/docs/subscriptions.mdx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ This guide covers how to implement subscriptions in GraphQL.js, when to use them
1212

1313
A subscription is a GraphQL operation that delivers ongoing results to the client when a specific event happens. Unlike queries or mutations, which return a single response, a subscription delivers data over time through a persistent connection.
1414

15-
GraphQL.js implements the subscription execution algorithm, but it's up to you to connect it to your event system and transport layer. Most implementations use WebSockets for transport, though any full-duplex protocol can work.
15+
GraphQL.js implements the subscription execution algorithm, but it's up to you to connect it to your event system and transport layer. Most implementations use WebSockets or SSE for transport, though any full-duplex protocol can work.
16+
You can find [here](https://the-guild.dev/graphql/yoga-server/docs/features/subscriptions#sse-vs-websocket) some valuable information about the differences between WebSockets and SSE.
1617

1718
## How execution works
1819

@@ -85,26 +86,26 @@ GraphQL.js supports subscription execution, but you’re responsible for setting
8586
- An event-publishing mechanism
8687
- A transport layer to maintain the connection
8788

88-
The following examples use the [`graphql-subscriptions`](https://github.com/apollographql/graphql-subscriptions) package to set up an in-memory event system.
89+
The following examples use the [`@graphql-yoga/subscriptions`](https://github.com/graphql-hive/graphql-yoga/tree/main/packages/subscription) package to set up an in-memory event system.
8990

9091
### Install dependencies
9192

9293
Start by installing the necessary packages:
9394

9495
```bash
95-
npm install graphql graphql-subscriptions
96+
npm install graphql @graphql-yoga/subscriptions
9697
```
9798

98-
To serve subscriptions over a network, you’ll also need a transport implementation. One option is [`graphql-ws`](https://github.com/enisdenjo/graphql-ws), a community-maintained WebSocket library. This guide focuses on schema-level implementation.
99+
To serve subscriptions over a network, you’ll also need a transport implementation. Two popular options are [`graphql-ws`](https://github.com/enisdenjo/graphql-ws), a community-maintained WebSocket library and [`graphql-sse`](https://github.com/enisdenjo/graphql-sse) a community-maintained Server-Sent Events library. This guide focuses on schema-level implementation.
99100

100101
### Set up a pub/sub instance
101102

102103
Create a `PubSub` instance to manage your in-memory event system:
103104

104105
```js
105-
import { PubSub } from 'graphql-subscriptions';
106+
import { createPubSub } from '@graphql-yoga/subscriptions'
106107

107-
const pubsub = new PubSub();
108+
const pubSub = createPubSub();
108109
```
109110

110111
This `pubsub` object provides `publish` and `asyncIterator` methods, allowing
@@ -124,7 +125,7 @@ const SubscriptionType = new GraphQLObjectType({
124125
fields: {
125126
messageSent: {
126127
type: GraphQLString,
127-
subscribe: () => pubsub.asyncIterator(['MESSAGE_SENT']),
128+
subscribe: () => pubsub.subscribe('MESSAGE_SENT'),
128129
},
129130
},
130131
});

0 commit comments

Comments
 (0)