Skip to content

Commit

Permalink
WIP: Fabo/use redis pubsub (#209)
Browse files Browse the repository at this point in the history
* use redis pubsub

* refactor

* Fix redis import
  • Loading branch information
faboweb authored Dec 16, 2019
1 parent 2de66e1 commit 0b0dd7f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/redis.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var url = require('url')
const { RedisCache } = require('apollo-server-cache-redis')
const { RedisPubSub } = require('graphql-redis-subscriptions')
const Redis = require('ioredis')
const config = require('../config')

let redis_uri = url.parse(config.redis_url || '')
Expand All @@ -20,4 +22,12 @@ function createCache() {
return new RedisCache(redisConfig)
}

function createRedisPubSub() {
return new RedisPubSub({
publisher: new Redis(redisConfig),
subscriber: new Redis(redisConfig)
})
}

exports.createCache = createCache
exports.createRedisPubSub = createRedisPubSub
16 changes: 15 additions & 1 deletion lib/subscriptions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { PubSub } = require('apollo-server')
const pubsub = new PubSub()
const config = require('../config')
const { createRedisPubSub } = require('./redis')

const pubsub = getPubSub()

const NEW_BLOCK = 'NEW_BLOCK'
const NEW_USER_TRANSACTION = 'NEW_USER_TRANSACTION'
Expand Down Expand Up @@ -46,3 +49,14 @@ module.exports = {
}
})
}

// the normal apollo server pub sub has limitations on the maximum of connections: https://stackoverflow.com/questions/43752073/graphql-subscriptions-max-listeners-exceeded-warning
// this is why we use Redis PubSub in production
function getPubSub() {
// default to EventEmitter pubsub
if (!config.redis_url) {
return new PubSub()
}

return createRedisPubSub()
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dotenv": "^8.2.0",
"firebase-admin": "^8.8.0",
"graphql": "^14.5.8",
"graphql-redis-subscriptions": "^2.1.1",
"lodash": "^4.17.15",
"websocket": "^1.0.30"
},
Expand Down
11 changes: 10 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2830,6 +2830,15 @@ graphql-extensions@^0.10.7:
apollo-server-env "^2.4.3"
apollo-server-types "^0.2.8"

graphql-redis-subscriptions@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/graphql-redis-subscriptions/-/graphql-redis-subscriptions-2.1.1.tgz#61d5cf36ccc74abbc596d640eadbddf2cd1a7a91"
integrity sha512-oBd6thADySxLpLEYEtUmpGTxGQhHCN8M+9gaGZaUic11UYEfCtCpvuP6vwx8p7R8uhGZ3dYG8dhEJ01ZMg8E/A==
dependencies:
iterall "^1.2.2"
optionalDependencies:
ioredis "^4.6.3"

graphql-subscriptions@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/graphql-subscriptions/-/graphql-subscriptions-1.1.0.tgz#5f2fa4233eda44cf7570526adfcf3c16937aef11"
Expand Down Expand Up @@ -3162,7 +3171,7 @@ invariant@^2.2.4:
dependencies:
loose-envify "^1.0.0"

ioredis@^4.0.0:
ioredis@^4.0.0, ioredis@^4.6.3:
version "4.14.1"
resolved "https://registry.yarnpkg.com/ioredis/-/ioredis-4.14.1.tgz#b73ded95fcf220f106d33125a92ef6213aa31318"
integrity sha512-94W+X//GHM+1GJvDk6JPc+8qlM7Dul+9K+lg3/aHixPN7ZGkW6qlvX0DG6At9hWtH2v3B32myfZqWoANUJYGJA==
Expand Down

0 comments on commit 0b0dd7f

Please sign in to comment.