We are using subscriptions on a node graphql server. Recently I added the cluster ability and used Redis for pubsub. On my local environment all works great. When I upload to heroku and use their Redis addon the first time I fire a mutation I see it show but the next time it does not. After a few attempts it works again (50% work).
import { RedisPubSub } from 'graphql-redis-subscriptions';
import Redis from 'ioredis';
let REDIS_URL = process.env.REDIS_URL || 'redis://127.0.0.1:6379';
const options = {
retryStrategy: times => {
// reconnect after
return Math.min(times * 50, 2000);
}
};
const pubsub = new RedisPubSub({
publisher: new Redis(REDIS_URL, options),
subscriber: new Redis(REDIS_URL, options)
});
Here is the error I get in the log
Aug 22 16:06:11 service-staging app[web] Error (node:76) UnhandledPromiseRejectionWarning: Error: Connection in subscriber mode, only subscriber commands may be used
Aug 22 16:06:11 service-staging app[web] info at Redis.sendCommand (/app/node_modules/ioredis/built/redis/index.js:570:24)
Aug 22 16:06:11 service-staging app[web] info at DatastoreShim.applySegment (/app/node_modules/newrelic/lib/shim/shim.js:1424:20)
Aug 22 16:06:11 service-staging app[web] info at _applyRecorderSegment (/app/node_modules/newrelic/lib/shim/shim.js:1015:20)
Aug 22 16:06:11 service-staging app[web] info at Redis._doRecord (/app/node_modules/newrelic/lib/shim/shim.js:994:17)
Aug 22 16:06:11 service-staging app[web] info at Redis.wrapper [as sendCommand] (/app/node_modules/newrelic/lib/shim/shim.js:965:24)
Aug 22 16:06:11 service-staging app[web] info at Redis.publish (/app/node_modules/ioredis/built/commander.js:124:21)
Aug 22 16:06:11 service-staging app[web] info at RedisPubSub.<anonymous> (/app/node_modules/graphql-redis-subscriptions/src/redis-pubsub.ts:64:31)
Aug 22 16:06:11 service-staging app[web] info at step (/app/node_modules/graphql-redis-subscriptions/dist/redis-pubsub.js:32:23)
Aug 22 16:06:11 service-staging app[web] info at Object.next (/app/node_modules/graphql-redis-subscriptions/dist/redis-pubsub.js:13:53)
Aug 22 16:06:11 service-staging app[web] info at /app/node_modules/graphql-redis-subscriptions/dist/redis-pubsub.js:7:71
Aug 22 16:06:11 service-staging app[web] info at new Promise (<anonymous>)
Aug 22 16:06:11 service-staging app[web] info at __awaiter (/app/node_modules/graphql-redis-subscriptions/dist/redis-pubsub.js:3:12)
Aug 22 16:06:11 service-staging app[web] info at RedisPubSub.publish (/app/node_modules/graphql-redis-subscriptions/dist/redis-pubsub.js:76:16)
Aug 22 16:06:11 service-staging app[web] info at publish (/app/src/resolvers/action.js:435:18)
Aug 22 16:06:11 service-staging app[web] info (node:76) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 9)
When I run the mutation I should see it show up for the listening subscription.
Also, on a side note if I set the number of instances to 1 or 2 I always get the error. 3 or more I at least get the first mutation and half of the following mutations.
After much personal pain and anguish I got it running. I removed the Heroku Redis add on and replaced it with the Redis Cloud add on. All works just as expected!