MongoDB: Server sockets closed after a few minutes I am working with multiple AWS intances connected to the same MongoDB database (inside Compose.io Elastic deployment). But i am getting the error server &lt;url&gt;:&lt;port&gt; sockets closed after a few minutes. Can anyone give me any hint about what may be wrong with the connection code?

CONNECTION CODE

var url = "mongodb://&lt;user&gt;:&lt;password&gt;@&lt;url1&gt;:&lt;port1&gt;,&lt;url2&gt;:&lt;port2&gt;/&lt;dbName&gt;?replicaSet=&lt;replicaSetName&gt;"; 
var options = { 
    server : {"socketOptions.keepAlive": 1}, 
    replSet : { "replicaSet": &lt;replicaSetName&gt;, "socketOptions.keepAlive": 1 } 
}; 
MongoClient.connect(url, options, function(err, db) { ... });


ERROR MESSAGE

Potentially unhandled rejection [2] MongoError: server &lt;url&gt;:&lt;port&gt; sockets closed 
at null. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:328:47) 
at g (events.js:199:16) 
at emit (events.js:110:17) 
at null. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:101:12) 
at g (events.js:199:16) 
at emit (events.js:110:17) 
at Socket. (/var/app/current/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:142:12) 
at Socket.g (events.js:199:16) 
at Socket.emit (events.js:107:17) 
at TCP.close (net.js:485:12)
First a probably silly question, are you replacing all the , , etc placeholders with the actual values from your compose.io database?

If you are already doing that, maybe you can debug the connection using the mongo shell to connect to your DB instances.

If you have local install of mongodb you can use:


  mongo host:port/database-name -u user -ppassword


This is also what compose.io docs recommend for connection debugging https://docs.compose.io/common-questions/how-to-connect-to-mongodb.html