GraphQL subscriptions is the official Apollo library for adding real-time capabilities to any GraphQL server via a PubSub pattern. Version 3.0.0 is stable, actively maintained, and ships TypeScript types. It works with any GraphQL.js-compatible server and client (not just Apollo). The package provides a lightweight PubSub implementation (EventEmitter-based for demos) and a `withFilter` helper for per-subscriber filtering. For production, you must swap the default PubSub for a store-backed implementation (e.g., Redis). It requires `graphql` ^15.7.2 or ^16 as a peer dependency and `es2018.asynciterable` in TypeScript configuration.
npm install graphql-subscriptionsNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to create a PubSub instance, define a subscription resolver with withFilter, and publish an event. Default PubSub is for demo only; replace for production.
Use a production-grade PubSub implementation like graphql-redis-subscriptions or graphql-mqtt-subscriptions.
Replace pubsub.subscribe(callback) with pubsub.asyncIterableIterator('TOPIC') in your subscribe resolver.You can continue using withFilter, but for scalability, implement custom async iterators.
Add 'es2018.asynciterable' to the 'lib' array in tsconfig.json.
Ensure the published object includes the key matching the subscription field name (e.g., { somethingChanged: { id: '123' } }).Replace `.asyncIterator()` with `.asyncIterableIterator()`.
Ensure graphql is version ^15.7.2 or ^16.0.0.
Return `pubsub.asyncIterableIterator(topic)` as the subscribe method.
Use:
Subscription: {
somethingChanged: {
subscribe: () => pubsub.asyncIterableIterator(SOMETHING_CHANGED_TOPIC),
},
}