Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:45:25+00:00 2026-06-17T12:45:25+00:00

I’m using nodejs and a mongoDB – and I’m having some connection issues. Well,

  • 0

I’m using nodejs and a mongoDB – and I’m having some connection issues.

Well, actually “wake” issues! It connects perfectly well – is super fast and I’m generally happy with the results.

My problem: If i don’t use the connection for a while (i say while, because the timeframe varies 5+ mins) it seems to stall. I don’t get disconnection events fired – it just hangs.

Eventually i get a response like Error: failed to connect to [ * .mongolab.com: * ] – ( * = masked values)

A quick restart of the app, and the connection’s great again. Sometimes, if i don’t restart the app, i can refresh and it reconnects happily.

This is why i think it is “wake” issues.

Rough outline of code:

I’ve not included the code – I don’t think it’s needed. It works (apart from the connection dropout)

Things to note: There is just the one “connect” – i never close it. I never reopen.

I’m using mongoose, socketio.

/* constants */

var mongoConnect = 'myworkingconnectionstring-includingDBname';


/* includes */

/* settings */

/* Schema */

var db = mongoose.connect(mongoConnect);

    /* Socketio */

io.configure(function (){
    io.set('authorization', function (handshakeData, callback) {

    });
});

io.sockets.on('connection', function (socket) {

});//sockets

io.sockets.on('disconnect', function(socket) {
    console.log('socket disconnection')
});

/* The Routing */

app.post('/login', function(req, res){  

});

app.get('/invited', function(req, res){

});

app.get('/', function(req, res){

});

app.get('/logout', function(req, res){

});

app.get('/error', function(req, res){

});

server.listen(port);
console.log('Listening on port '+port);

db.connection.on('error', function(err) {
    console.log("DB connection Error: "+err);
});
db.connection.on('open', function() {
    console.log("DB connected");
});
db.connection.on('close', function(str) {
    console.log("DB disconnected: "+str);
});

I have tried various configs here, like opening and closing all the time – I believe though, the general consensus is to do as i am with one open wrapping the lot. ??

I have tried a connection tester, that keeps checking the status of the connection… even though this appears to say everthing’s ok – the issue still happens.

I have had this issue from day one. I have always hosted the MongoDB with MongoLab.
The problem appears to be worse on localhost. But i still have the issue on Azure and now nodejit.su.

As it happens everywhere – it must be me, MongoDB, or mongolab.

Incidentally i have had a similar experience with the php driver too. (to confirm this is on nodejs though)

It would be great for some help – even if someone just says “this is normal”

thanks in advance

Rob

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:45:26+00:00Added an answer on June 17, 2026 at 12:45 pm

    Thanks for all the help guys – I have managed to solve this issue on both localhost and deployed to a live server.

    Here is my now working connect code:

    var MONGO = {
        username: "username",
        password: "pa55W0rd!",
        server: '******.mongolab.com',
        port: '*****',
        db: 'dbname',
        connectionString: function(){
            return 'mongodb://'+this.username+':'+this.password+'@'+this.server+':'+this.port+'/'+this.db;
        },
        options: {
            server:{
                auto_reconnect: true,
                socketOptions:{
                    connectTimeoutMS:3600000,
                    keepAlive:3600000,
                    socketTimeoutMS:3600000
                }
            }
        }
    };
    
    var db = mongoose.createConnection(MONGO.connectionString(), MONGO.options);
    
    db.on('error', function(err) {
        console.log("DB connection Error: "+err);
    });
    db.on('open', function() {
        console.log("DB connected");
    });
    db.on('close', function(str) {
        console.log("DB disconnected: "+str);
    });
    

    I think the biggest change was to use “createConnection” over “connect” – I had used this before, but maybe the options help now. This article helped a lot http://journal.michaelahlers.org/2012/12/building-with-nodejs-persistence.html

    If I’m honest I’m not overly sure on why I have added those options – as mentioned by @jareed, i also found some people having success with “MaxConnectionIdleTime” – but as far as i can see the javascript driver doesn’t have this option: this was my attempt at trying to replicate the behavior.

    So far so good – hope this helps someone.

    UPDATE: 18 April 2013 note, this is a second app with a different setup

    Now I thought i had this solved but the problem rose it’s ugly head again on another app recently – with the same connection code. Confused!!!

    However the set up was slightly different…

    This new app was running on a windows box using IISNode. I didn’t see this as significant initially.

    I read there were possibly some issues with mongo on Azure (@jareed), so I moved the DB to AWS – still the problem persisted.

    So i started playing about with that options object again, reading up quite a lot on it. Came to this conclusion:

    options: {
        server:{
            auto_reconnect: true,
            poolSize: 10,
            socketOptions:{
                keepAlive: 1
            }
        },
        db: {
            numberOfRetries: 10,
            retryMiliSeconds: 1000
        }
    }
    

    That was a bit more educated that my original options object i state.
    However – it’s still no good.

    Now, for some reason i had to get off that windows box (something to do with a module not compiling on it) – it was easier to move than spend another week trying to get it to work.

    So i moved my app to nodejitsu. Low and behold my connection stayed alive! Woo!

    So…. what does this mean… I have no idea! What i do know is is those options seem to work on Nodejitsu…. for me.

    I believe IISNode uses some kind of “forever” script for keeping the app alive. Now to be fair the app doesn’t crash for this to kick in, but i think there must be some kind of “app cycle” that is refreshed constantly – this is how it can do continuous deployment (ftp code up, no need to restart app) – maybe this is a factor; but i’m just guessing now.

    Of course all this means now, is this isn’t solved. It’s still not solved. It’s just solved for me in my setup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.