ioredis Unhandled error event: Error: connect ETIMEDOUT - node.js

Has anyone encountered below error while connecting to standalone redis server using node js ioredis package?
Below is the error stack trace:
2018-08-16T10:52:18.351869060Z [ioredis] Unhandled error event: Error: connect ETIMEDOUT
2018-08-16T10:52:07.449457296Z at Timer.listOnTimeout (timers.js:207:5)
2018-08-16T10:52:07.449448499Z at tryOnTimeout (timers.js:237:5)
2018-08-16T10:52:07.449439722Z at ontimeout (timers.js:365:14)
2018-08-16T10:52:07.449430834Z at Socket._onTimeout (net.js:339:8)
2018-08-16T10:52:07.449421915Z at Socket.emit (events.js:185:7)
2018-08-16T10:52:07.449413002Z at emitNone (events.js:86:13)
2018-08-16T10:52:07.449403458Z at Socket.g (events.js:291:16)
This is occurring for instantiating only Standalone Redis object in node js. Below is the code I am using,
var publisher = new redis(redisPort, redisHost);
any solution would be highly appreciated.

You can probably try increasing the timeout limit since ioredis has a default timeout value.
Normally we would have it set as,
new Redis({
connectTimeout: 10000
})
In your case, since you have,
var publisher = new redis(redisPort, redisHost);
You will have to edit your code to pass the connectTimeout parameter to be passed accordingly.
Hope this helps.

It's a bit late but can be helpful in the future for somebody else.
const redis = new Redis({
port: <your_redis_port>,
host: <your_redis_hostname>,
connectTimeout: 10000
});

I am using Redis client maintained by Heroku and for some strange reason the Redis credentials have been changed by Heroku, I have been facing the same issue until I double-check the old credentials and the one on Heroku and I realised they were no longer the same then copied the new one from Heroku and pasted it in my .env file and everything is working as expected now!!!

Related

Redis connection to x.x.x.x:6379 failed - write ECONNRESET. NodeJS GCP Standard Environment

The redis get calls sometimes fails with the following error. Once this happens, as a follow through, many of the requests fail/timeout intermittently with a 502 status.
{ Error: Redis connection to X.X.X.X:6379 failed - write ECONNRESET
at afterWriteDispatched (internal/stream_base_commons.js:78:25)
at writeGeneric (internal/stream_base_commons.js:73:3)
at Socket._writeGeneric (net.js:714:5)
at Socket._write (net.js:726:8)
at doWrite (_stream_writable.js:415:12)
at writeOrBuffer (_stream_writable.js:399:5)
at Socket.Writable.write (_stream_writable.js:299:11)
at RedisClient.write (/workspace/node_modules/redis/index.js:949:43)
at RedisClient.internal_send_command (/workspace/node_modules/redis/index.js:885:14)
at RedisClient.internal_send_command_trace [as internal_send_command] (/workspace/node_modules/#google-cloud/trace-agent/build/src/plugins/plugin-redis.js:91:50)
at RedisClient.get (/workspace/node_modules/redis/lib/commands.js:46:25)
at Promise (internal/util.js:274:30)
at new Promise (<anonymous>)
at RedisClient.get (internal/util.js:273:12)
at ConversationQueueServices.startTimeoutToClearQueueForVisitor (/workspace/ls/Services/ConversationQueueServices.js:306:57)
at PeoplesService.onVisitorSubscriberLeft (/workspace/ls/Services/PeoplesService.js:51:31)
at app.post (/workspace/app.js:196:27)
at Layer.handle [as handle_request] (/workspace/node_modules/express/lib/router/layer.js:95:5)
at next (/workspace/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/workspace/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/workspace/node_modules/express/lib/router/layer.js:95:5)
at /workspace/node_modules/express/lib/router/index.js:281:22 errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'write' }
From the looks of it, ECONNRESET indicates the connection is closed by redis instance. There is no surge of traffic of any sort. cpu, memory and number of connections are as usual.
Will moving to updated redis npm client may help? Currently using 3.0.2 redis client version
Any help is much appreciated. Thanks in advance.
ECONNRESET error is thrown usually when the other end of the connection which could be TCP connected node that is communicating with the server or calling out and the socket timeout results in no response received which results in the sender closing it with the timeout exception handling type.
You can try to add retries or increase the timeout for the redis connection configurations.
Try running through this tutorial which might be helpful for troubleshooting the Redis Setup.
Please check a similar example with details node-js-best-practice-exception-handling.

Error: No event 'socketConnect' in state 'SentPrelogin'

I am trying to connect my SQL Server database with node.js using knex but I am facing issue
Error: No event 'socketConnect' in state 'SentPrelogin'
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:26)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
at C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1145:12
at Socket.onConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connector.js:106:7)
at Socket.emit (events.js:314:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
Emitted 'error' event on Connection instance at:
at Connection.dispatchEvent (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1281:12)
at Connection.socketConnect (C:\Users\temp\Documents\PRactice\node_modules\tedious\lib\connection.js:1303:10)
[... lines matching original stack trace ...]
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1131:10)
[nodemon] app crashed - waiting for file changes before starting...
My code is
var knex = require('knex')({
client: 'mssql',
version:"7_1",
connection: {
user: 'sa',
password: 'Admin#123',
server: 'localhost',
database: 'Demo'
}
});
knex.select("*").from("Country")
.then(function (depts){
depts.forEach((dept)=>{ //use of Arrow Function
console.log({...dept});
});
}).catch(function(err) {
// All the error can be checked in this piece of code
console.log(err);
}).finally(function() {
// To close the connection pool
knex.destroy();
});
You need to add a missing dependency:
npm install --save tedious
As of knex v0.95.0 you'll need to use the tedious library instead of mssql when connecting to an MSSQL database. According to the knex upgrade instructions:
MSSQL driver was completely reworked in order to address the multitude of connection pool, error handling and performance issues. Since the new implementation uses tedious library directly instead of mssql, please replace mssql with tedious in your dependencies if you are using a MSSQL database.
Installing the package above should resolve your issue. I also had to set the encrypt option to false when connecting to my local database to avoid this error:
ConnectionError: Failed to connect to localhost:1433 - self signed certificate

Nodejs Bull queue not connecting to Azure

When changing the settings inside the new bull Queue object, I get an error in the console. When running Bull Queue locally the application works perfectly fine. As soon as I change the credentials to Azure, I get the error below. When running locally I run the redis-server but not when using the Azure credentials.
I have tried the example tutorial, on the Azure website, with nodejs and the redis npm package, and the Azure redis cache works perfectly fine. Therefore, I am left to believe that I am doing something wrong in the config. I have also tried adding "maxRetriesPerRequest" and "enableReadyCheck" to the redis object however, they have had no effect. I also make sure I execute the done function within the process function.
const queue = new Queue('sendQueue', {
defaultJobOptions: { removeOnComplete: true },
redis: {
port: env.AZURE_REDIS_PORT,
host: env.AZURE_REDIS_HOST,
password: env.AZURE_REDIS_PASSWORD
},
});
at Queue.<anonymous> (/Users/abc/Projects/Sean/dist/tasks/sendQueue.js:47:11)
at Queue.emit (events.js:208:15)
at Redis.emit (events.js:203:13)
at Redis.silentEmit (/Users/abc/Projects/Sean/node_modules/ioredis/built/redis/index.js:482:26)
at Socket.<anonymous> (/Users/abc/Projects/Sean/node_modules/ioredis/built/redis/event_handler.js:122:14)
at Object.onceWrapper (events.js:291:20)
at Socket.emit (events.js:203:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at processTicksAndRejections (internal/process/task_queues.js:77:11)
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:183:27)
Try to add configuration for TLS when using Azure redis cache. Should be the same config value as host. I did not manage to get a connection without it.
var notificationQueue = new Queue('notifications', {
redis: {
port: Number(process.env.REDIS_PORT),
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASS,
tls: {
servername: process.env.REDIS_HOST
}
}});

MongoDB fails to respond on localhost but works for another node app

I'm working on a new node.js app on an old server I use for dev stuff.
Both apps have the mongo URL specified:
var mongoUri = 'mongodb://localhost/node';
but on startup, the old node.js app works fine, but the new one doesn't. The new one returns the following for node server.js
Error: unable to connect to database at mongodb://localhost/node
at NativeConnection.<anonymous> (/var/www/server/server.js:15:9)
at emitOne (events.js:115:13)
at NativeConnection.emit (events.js:210:7)
at /var/www/server/node_modules/mongoose/lib/connection.js:336:19
at args.push (/var/www/server/node_modules/mongodb/lib/utils.js:403:25)
at /var/www/server/node_modules/mongodb/lib/mongo_client.js:254:21
at connectCallback (/var/www/server/node_modules/mongodb/lib/mongo_client.js:933:5)
at /var/www/server/node_modules/mongodb/lib/mongo_client.js:782:11
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Any thoughts or suggestions would be really appreciated. I've tried everything short of reinstalling mongo at this point :(

How to troubleshoot ECONNREFUSED error?

I'm trying to get the d3.js demo given here to work.
I'm following along the instructions in the README.md file. I get the npm install step to work, but the next step, node server, fails:
% node server
Master pid 16196
16197 listening. Go to: http://localhost:3030/
events.js:72
throw er; // Unhandled 'error' event
^
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
at RedisClient.on_error (/Users/yrstruly/tmp/derby-barchart/node_modules/redis/index.js:189:24)
at Socket.<anonymous> (/Users/yrstruly/tmp/derby-barchart/node_modules/redis/index.js:95:14)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:440:14
at process._tickCallback (node.js:415:13)
It looks like the code is expecting to have some process listening to port 6379, but the README says nothing about this.
I'm quite unfamiliar with most of the software used by this demo. In particular, I know very little about node.js, derby, and redis. Therefore, I'm following the steps in the README rather blindly. Any help with troubleshooting this error would be appreciated.

Resources