Heroku RedisCloud connexions on "Realtime Online User Tracking with ActionCable"
I'm running in a weird issue.
Following the Realtime Online User Tracking with ActionCable screencast, in an app using rails 5.2 I used:
ActionCable.server.pubsub.redis_connection_for_subscriptions.sadd ActionCable.server.pubsub.redis_connection_for_subscriptions.rem ActionCable.server.pubsub.redis_connection_for_subscriptions.smembers
to add, remove and retrieve a users ids list from redis.
Deploying to staging on heroku, using the rediscloud addon for redis, I had the following issue with this implementation, each of this call was instantiating a new connexion to redis, so that I was hitting the rediscloud connexion limit really fast.
does anyone has this kind of issue ? I cannot replicate the output I got from redis cloud in redis-cli
CLIENT LIST in my local env. So I'm wondering if it has something to do with redis cloud ?
I read here to use
ActionCable.server.pubsub.send(:redis_connection) to get the ActionCable connexion but I looked into the code for the ActionCable Redis adapater and, I'm not totally sure but think that's what the initial method
ActionCable.server.pubsub.redis_connection_for_subscriptions indeed instantiate a new connexion each time it is called?
I'm kind of worried about my current solution:
I resolved the problem by doing inspired by the rediscloud doc this in an initializer
$redis = Redis.new(url: ENV['REDIS_URL')
and replacing the initial code by:
$redis.sadd $redis.srem $redis.smembers
It now works as expected and I don't reach the connexion limit, but I'm quite not sure about the use of a global variable.
If anybody has an insight / a better way to solve this I would really appreciate it.
thanks for reading that long post :)