select correct redis database on reconnect

327 views Asked by At

I have a node application that uses node_redis. Upon application start, a redis client is created, the correct database is selected once, and then this client is used for every request:

this.redis = redis.createClient(port, host);
this.redis.on('error', function(err) { console.log('Redis error: ' + err); });
this.redis.send_command('SELECT', [database]);

Now the problem is: when the connection goes away (for example when redis is manually restarted), node_redis automatically re-connects, but it's on the wrong database, since the SELECTwas not run after re-connecting. I cannot find how to detect this, since the error event also doesn't fire in this case. What should I do?

0

There are 0 answers