Struggling with a persistent issue here. Despite reading numerous documents and posts from others on the same topic, I can't seem to find a solution to prevent this problem. I am intentionally shutting down the redis server to avoid potential disasters in production, but my attempts have been unsuccessful so far. Below is a simplified version of my current code...
var cluster = require('cluster');
var express = require('express');
var http = require('http');
var redis = require('redis');
var redisAdapter = require('socket.io-redis');
var sticky = require('sticky-session');
var port = process.env.PORT || 3333;
var workers = 3;
var app = express();
var server = http.createServer(app);
var io = require('socket.io')(server);
if(!sticky.listen(server, 3333, {workers: workers})) {
server.once('listening', () => {
console.log('server started on port ' + port);
process.on('exit', () => {
io.emit('error', {errorType: 'SERVER_SHUTDOWN'});
});
process.on('uncaughtException', (err) => {
console.log('uncaught exception occurred');
console.log(err);
});
});
} else {
addRedisAdapter(io);
addIOEventHandler(io);
}
function addRedisAdapter(io) {
var redisUrl = process.env.REDISTOGO_URL || 'redis://127.0.0.1:6379';
var redisOptions = require('parse-redis-url')(redis).parse(redisUrl);
var pub = redis.createClient(redisOptions.port, redisOptions.host, { returnBuffers: true});
var sub = redis.createClient(redisOptions.port, redisOptions.host, {returnBuffers: true});
io.on('error', (err) => {
console.log(err);
});
io.on('uncaughtException', (err) => {
console.log(err);
});
io.adapter(redisAdapter({pubClient: pub, subClient: sub}));
console.log('redis adapter started');
}
function addIOEventHandler(io) {
//all the events for io
}
The sequence starts with initiating the redis-server followed by launching node.js with this server. Afterward, in a terminal window, I execute "redis-cli shutdown" to forcefully bring down the redis server. Immediately after that, the node.js terminal goes into a frenzy, repeatedly displaying the same error message.
events.js:154
throw er; // Unhandled 'error' event
^
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:893:11)
at exports._exceptionWithHostPort (util.js:916:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1075:14)
redis adapter started with url: redis://127.0.0.1:6379
Despite implementing handlers like process.on('uncaughtException', io.on('uncaughtException', and io.on('error', the server still loops through saying there is an "Unhandled 'error' event". Any assistance would be greatly appreciated as I've exhausted all resources available online without success.