use different PubSubEngines (e.g. RedisPubSub, PostgresPubSub) for different topics
npm install graphql-multiplex-subscriptions




If you need to use multiple PubSubEngines in your Apollo server (e.g.graphql-redis-subscriptions and graphql-postgres-subscriptions), use this
package to coordinate which PubSubEngine handles a given topic.
Imagine you're currently using graphql-redis-subscriptions, and you set up
your pubsub like this:
``js
// Before
import { RedisPubSub } from 'graphql-redis-subscriptions'
export const pubsub = new RedisPubSub()
`
But then you decide you want to use graphql-postgres-subscriptions for someMultiplexPubSub
topics. All you have to do is create a with a selectEnginepg/
function that returns which engine you'd like to use for a given topic. For
example, you could use postgres for all topics beginning with :
`js
import { RedisPubSub } from 'graphql-redis-subscriptions'
import { PostgresPubSub } from 'graphql-postgres-subscriptions'
import { MultiplexPubSub } from 'graphql-multiplex-subscriptions'
const redisPubSub = new RedisPubSub()
const postgresPubSub = new PostgresPubSub()
export const pubsub = new MultiplexPubSub({
selectEngine: topic =>
topic.startsWith('pg/') ? postgresPubSub : redisPubSub,
})
`
WARNING: selectEngine should always return a PubSubEngine. If it doesn't,TypeError
you will get s!
Since you use a function to select the engine you can use whatever logic you
want. So you could just map specific topics to a specific engine:
`js
import { RedisPubSub } from 'graphql-redis-subscriptions'
import { PostgresPubSub } from 'graphql-postgres-subscriptions'
import { MultiplexPubSub } from 'graphql-multiplex-subscriptions'
const redisPubSub = new RedisPubSub()
const postgresPubSub = new PostgresPubSub()
export const pubsub = new MultiplexPubSub({
selectEngine: topic =>
topic.matches(/^(user|organization)\//) ? postgresPubSub : redisPubSub,
})
``