How do GraphQL subscriptions work over WebSocket?
Answer
GraphQL subscriptions use a persistent WebSocket connection between client and server. The client sends a subscription document over the WebSocket, and the server registers a listener on a pub/sub system (e.g., Redis Pub/Sub, in-memory EventEmitter). When the triggering event occurs (e.g., a new message is created), the server publishes to the channel, the subscription resolver is executed for each subscriber, and the resulting data is pushed to the client over their WebSocket. The graphql-ws protocol (or the older subscriptions-transport-ws) standardizes the handshake and message format. Apollo Server and other servers implement this protocol out of the box.
Previous
What is schema stitching versus Apollo Federation in GraphQL?
Next
What is query depth limiting in GraphQL?
More GraphQL Questions
View all →- Intermediate What is the N+1 problem in GraphQL and how does DataLoader solve it?
- Intermediate What are persisted queries in GraphQL?
- Intermediate What is the difference between schema-first and code-first GraphQL approaches?
- Intermediate What are the pagination strategies in GraphQL?
- Intermediate How is authentication handled in GraphQL?