What is GraphQL subscriptions with WebSocket protocol graphql-ws vs subscriptions-transport-ws?
Answer
Two WebSocket protocols for GraphQL subscriptions: subscriptions-transport-ws: the original library (by Apollo), using the graphql-ws (confusingly different from the library below) sub-protocol string. It is now deprecated and unmaintained. Known issues: connection problems, unclear error handling, protocol design issues. graphql-ws library: the modern, actively maintained replacement by Denis Badurina. Uses the graphql-transport-ws sub-protocol. Key improvements: clear connection lifecycle (Connected → Ping/Pong → Subscribe → Next/Error/Complete), proper error handling, lazy connection mode, reconnection support, and active maintenance. Apollo Client 3.5+ supports graphql-ws: const wsLink = new GraphQLWsLink(createClient({ url: 'ws://localhost:4000/subscriptions' }));. Server setup: useServer({ schema }, wsServer);. Always use graphql-ws for new projects. Migrate existing projects away from subscriptions-transport-ws.
Previous
What is the GraphQL Codegen tool and how is it used?
Next
How do you implement distributed tracing for GraphQL?
More GraphQL Questions
View all →- Advanced What is Apollo Federation v2 and how does the supergraph work?
- Advanced How do you implement entity resolution in Apollo Federation?
- Advanced What are the security vulnerabilities specific to GraphQL?
- Advanced How does the GraphQL query planning and execution pipeline work?
- Advanced What is the @defer and @stream directive and how do they work?