You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adding some suggestions to the subscriptions section:
As mentioned in [this
comment](#4406 (comment)),
the `graphql-subscriptions` library is not maintained
([reference](apollographql/graphql-subscriptions#240))
I've made a change to recommend another library for the solution.
In order to move this our of draft, @enisdenjo@ardatan I believe we'll
need to publish a README specifically for that library.
I've also changed the comment about WebSockets and SSE, I'm not sure if
we want to say that most people use WebSockets?
---------
Co-authored-by: Yaacov Rydzinski <yaacovCR@gmail.com>
Copy file name to clipboardExpand all lines: website/pages/docs/subscriptions.mdx
+43-13Lines changed: 43 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,8 @@ This guide covers how to implement subscriptions in GraphQL.js, when to use them
12
12
13
13
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.
14
14
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. Common transport options include WebSockets and Server-Sent Events (SSE).
16
+
For more context on the advantages of each approach, consider a [general comparison of WebSockets and SSE](https://websocket.org/comparisons/sse/) as well as [GraphQL Yoga's breakdown with respect to GraphQL](https://the-guild.dev/graphql/yoga-server/docs/features/subscriptions#sse-vs-websocket).
16
17
17
18
## How execution works
18
19
@@ -85,30 +86,59 @@ GraphQL.js supports subscription execution, but you’re responsible for setting
85
86
- An event-publishing mechanism
86
87
- A transport layer to maintain the connection
87
88
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 a small in-memory pub/sub helper to set up an event
90
+
system for local development.
89
91
90
92
### Install dependencies
91
93
92
94
Start by installing the necessary packages:
93
95
94
96
```bash
95
-
npm install graphql graphql-subscriptions
97
+
npm install graphql
96
98
```
97
99
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.
100
+
To serve subscriptions over a network, you’ll also need a transport implementation. Two common 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.
99
101
100
102
### Set up a pub/sub instance
101
103
102
-
Create a `PubSub` instance to manage your in-memory event system:
104
+
In production, subscriptions are typically backed by a robust pub/sub system
105
+
that can span processes and servers. For this demonstration, you can use an
106
+
in-memory pub/sub system of your choice, or an implementation such as the
0 commit comments