How to restore redis connection?

I have a simple example:

var redis = require('redis'),
client = redis.createClient();

var test = function() {
    client.brpop('log', 0, function(err, reply) {
        if (err != null ) {
            console.log(err);
        }    else {
            .... parse log string ....
        }
        test();
    });
}

test();

How to reconnect a redis connection after rebooting a redis server?

+5
source share
1 answer

The Redis client automatically connects. Just make sure you handle the event "error"from the client. By example :

var redis = require('redis');
client = redis.createClient();
client.on('error', function(err){ 
  console.error('Redis error:', err); 
});

Otherwise, this code is the beginning of the process.

this.emit("error", new Error(message));
// "error" events get turned into exceptions if they aren't listened for.  If the user handled this error
// then we should try to reconnect.
this.connection_gone("error");

Further on the client is executed . connection_gone () .

Please note that you can also listen to an event "reconnecting"that will be notified when this happens.

+12
source

All Articles