mongodb connection timeout when mongodb is not in the same VM - node.js

I have my MongoDB running on a Cloud VM , and NodeJS Server running on another Azure Cloud VM. I use Mongoose in the Application for MongoDB Connect. Keep getting this timeout issue every day? Any suggestions
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [XXXXX.cloudapp.net:27017]
at null.<anonymous> (/app/satwebnodev2/satwebnodev2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:546:74)
at emit (events.js:106:17)
at null.<anonymous> (/app/satwebnodev2/satwebnodev2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:150:15)
at emit (events.js:98:17)
at Socket.<anonymous> (/app/satwebnodev2/satwebnodev2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:533:10)
at Socket.emit (events.js:95:17)
at net.js:440:14
at process._tickCallback (node.js:419:13)

Refer this link :- http://www.slideshare.net/VictoriaMalaya1/mongo-db-in-windows-azure
MongoDB in Windows Azure:-
Drawbacks and workarounds : connection timeout for .Net driver MongoDB Client Uses .Net driver socket timeout exception Windows Azure terminates an inactive connection after roughly 4 minutes of idling MongoDB suggests: Set the max idle connection time on the driver: MongoDefaults.MaxConnectionIdleTime = TimeSpan.FromMinutes(1); My experience: Add following options to connection string: maxIdleTimeMS=60000;connectTimeoutMS=60000;socketTimeoutMS=60000 Sample of connection string: connectionString="mongodb://localhost/mydb ?maxIdleTimeMS=60000;connectTimeoutMS=60000;socketTimeoutMS=60000"

Related

How to fix connect EADDRINUSE error in azure function

I have a node application running in an azure function that will read json files from the data lake. When I try to run the function I get the following error connect EADDRINUSE 52.7.45.242:443at module.exports (C:\home\site\wwwroot\InitialLoadFunction\index.js:37:15)at runMicrotasks (<anonymous>)at processTicksAndRejections (internal/process/task_queues.js:93:5)
I have restarted/stopped the function app and re-deployed the code but the error will not resolve. Anyone know how to clear the process that is using this address in azure?
EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.
So, in your case, there must be running a server on specific port already. Try changing the port or create a new function app and deploy.

Nodejs App Engine Standard env Redis error read ECONNRESET

I am running my Nodejs app on the standard environment and in every 4 minutes my redis connects throws error and then connects again,
The error is this -
"{ Error: Redis connection to xx.xxx.x.x:6379 failed - read ECONNRESET at TCP.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' }"
But when I am running my app same Redis and node version in compute engine OR in flexible env then in both env I am not getting this error.
I have seen solutions on many websites where they are creating error handler for redis and then keeping nodeJs app from crash but the thing is why is this error happening in the first place, If this issue persists then I think it will impact my redis query performance and will cause huge delays.
I am not sure if you are using an external Redis instance for your application or Cloud Memorystore. These ECONNRESET errors just happens when the connection gets interrupted when contacting an endpoint. I would suggest to check what is happening in the redis instance logs when the endpoint is reached. It could be that the process that calls Redis ends too soon which if that's is the case you need to set the timeout setting in the redis.conf to something other than 0.

How to reproduce and prevent ESOCKETTIMEDOUT in Node JS

At some random times, on our production server, we face such type of timeouts and the server crashes.
Error: ESOCKETTIMEDOUT
at ClientRequest.<anonymous> (/usr/src/app/node_modules/request/request.js:816:19)
at Object.onceWrapper (events.js:272:13)
at ClientRequest.emit (events.js:180:13)
at ClientRequest.emit (domain.js:422:20)
at TLSSocket.emitTimeout (_http_client.js:703:34)
at Object.onceWrapper (events.js:272:13)
at TLSSocket.emit (events.js:180:13)
at TLSSocket.emit (domain.js:422:20)
at TLSSocket.Socket._onTimeout (net.js:396:8)
at ontimeout (timers.js:466:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:267:5)
I have gone though a lot of blogs and QAs. To summarize, it's read timeout when the caller doesn't get next bytes of response within some specific time limit. From the offical docs (request-npm), we can see -
Read timeout: Time to wait for a server to send response headers (and start the response body) before aborting the request.
Connection timeout: Sets the socket to timeout after timeout milliseconds of inactivity. Note that increasing the timeout beyond the OS-wide TCP connection timeout will not have any effect (the default in Linux can be anywhere from 20-120 seconds)
However I am trying to reproduce it in local linux machine with a HTTP request to my own Node JS server. I am able to get ETIMEDOUT which is connection timeout but I can't get ESOCKETTIMEDOUT error reproduced. In my destination server I had used this simple script with Express JS as an attempt to get this error.
app.use((req, res, next) => {
res.write('sample string');
// Do nothing, let the req wait
});
Therefore does anybody knows a simple approach to get this reproduced? Also I would like to know how to handle this error instead of having it as uncaught exception.
Thanks!

Connecting to a different MySQL server through Sails JS

I have a use case where I need to fetch data from two different DBs placed on different server(one of them being my own localhost/dev/prod). On providing details for connection using adapter : 'sails-mysql', whenever i try & access the DB(for a simple select query), it throws this exception:
debug: err, response -- Error: Could not connect to MySQL:
Error: Handshake inactivity timeout
at afterwards (/Users/xx/code/xx/xx/node_modules/sails-mysql/lib/connections/spawn.js:72:13)
at /Users/xx/code/xx/xx/node_modules/sails-mysql/lib/connections/spawn.js:40:7
at Handshake.onConnect [as _callback] (/Users/xx/code/xx/xx/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:54:9)
at Handshake.Sequence.end (/Users/xx/code/xx/xx/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at /Users/xx/code/xx/xx/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:399:18
at Array.forEach (<anonymous>)
at /Users/xx/code/xx/xx/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:398:13
at process._tickCallback (internal/process/next_tick.js:112:11) undefined
Example of connections.js:
MySQLServerForDataServer: {
adapter: 'sails-mysql',
host: (process.env.DBNAME_DATA_SERVER_HOST),
user: (process.env.DBNAME_DATA_SERVER_USER),
password: (process.env.DBNAME_DATA_SERVER_PWD),
database: (process.env.DBNAME_DATA_SERVER_DB)
}
I'm able to access this server through Valentina Studio or whatever(ssh key is added on there server too), but through code, it's not letting me through.
Can anyone suggest something which might help me connect to this DB Server?

Socket.io Node.js how to send a socket emit to a specific user?

I know about this question:
Send message to specific client with socket.io and node.js
However I tried any of the answers there and I receive the next error:
/Users/razbuchnik/node/routes/sockets.js:13
io.to["1ZAJEIGhcMP2k6zqAAAA"].emit("socketFromServer", info);
^
TypeError: Cannot read property 'emit' of undefined
at Socket.<anonymous> (/Users/razbuchnik/node/routes/sockets.js:13:37)
at Socket.emit (events.js:159:13)
at /Users/razbuchnik/node/node_modules/socket.io/lib/socket.js:513:12
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
[nodemon] app crashed - waiting for file changes before starting...
You can user io.to, but first you have to join the room as specified in the documentation. Read more on https://socket.io/docs/rooms-and-namespaces/#joining-and-leaving.
io.on('connection', function(socket){
socket.join('some room');
});
and only then
io.to('some room').emit('some event');

Resources