nodejs terminates on redis failure
Asked Answered
C

3

7

I am using Node.js, Express, Redis and Socket.io. When Redis is down, Node.js will terminate.

How can I prevent this, probably somewhere to code reconnection or something?

Output:

info - socket.io started
Express server listening on port 3000

Error:

events.js:72
        throw er; // Unhandled 'error' event
              ^ Error: Redis connection to 127.0.0.1:6380 failed - connect ECONNREFUSED
    at RedisClient.on_error (...../node_modules/redis/index.js:151:24)
    at Socket.<anonymous> (...../node_modules/redis/index.js:86:14)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:426:14
    at process._tickCallback (node.js:415:13)
Complicate answered 1/9, 2013 at 12:45 Comment(3)
sorry about the error messages. not sure how to format them here.Complicate
it would be good if you could mark the below answer as accepted - it is a useful answer.Kussell
It has been a long time. I can't remember much. Do you think this is the answer? If so I will mark it.Complicate
A
16

It seems like you don't have an error handler for your redis client instance. Can you provide some excerpt from your code? Probably we can help you better that way. Just an error message is a bit less.

If I had to guess then I'd say you have no error handler...

See here: https://github.com/mranney/node_redis#usage

Do you have a on error callback?

Example:

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


// This is required so your error doesn't bubble
// upwards and kills your instance

client.on("error", function (err) {
    console.log("Error " + err);
});

The node_redis client automatically reconnects so you don't need to handle that although you can configure a max_attempts and other options. See here: https://github.com/mranney/node_redis#rediscreateclientport-host-options

I hope I could provide some useful information

Arbour answered 1/9, 2013 at 16:10 Comment(1)
its not about redis client, its about socket.io redis adapter.Brandtr
B
0

if you are using socket.io redisAdapter, then do this after creating your socket.io server :

io.of('/').adapter.on('error', (err)=>{console.log('adapter error',err)});
Brandtr answered 17/11, 2021 at 10:9 Comment(0)
O
0

I added this and it worked:

redisClient.on('connect', function(){
    console.log('Connected to Redis');
});

redisClient.on('error', function(err) {
     console.log('Redis error: ' + err);
});
Orvie answered 14/4, 2022 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.