Listen for Reconnecting Events in MongoDB Driver

I would like to add event listeners to the MongoDB connection to trigger something when the connection drops, every reconnect attempt and successful reconnection attempt.

I have read all the official docs and APIs, but I cannot find a solution.

I currently have this, but only timeouts are working. // If we haven't initialized "MongoClient" yet, initialize it and save it. if (! this.client) this.client = new MongoClient ();

    this.connection = await this.client.connect(connectionString, this.settings);

    this.client.server.on('connect', event => {
        console.log(event);
    });

    this.client.server.on('error', event => {
        console.log(event);
    });

    this.client.server.on('reconnect', event => {
        console.log(event);
    });

    this.client.server.on('connections', event => {
        console.log(event);
    });

    this.client.server.on('timeout', event => {
        console.log(event);
    });

    this.client.server.on('all', event => {
        console.log(event);
    });

      

I've tried the events listed here and they work, but there is no "reconnect" event: http://mongodb.github.io/node-mongodb-native/2.2/reference/management/sdam-monitoring/

+3


source to share


1 answer


Sure. Basically, though, you need to enforce the EventEmitter at a lower level than basically outside of itself MongoClient

.

You can clearly see that such things exist as they are visible in the "logging" which can be enabled in the driver by setting:

{ "loggerLevel": "info" }

      

From now on, it's just a matter of using the actual emitter source. I did this in the following listing, and also included a little trick to get the listed events from a given emitter, which I admittedly used to track this:



const MongoClient = require('mongodb').MongoClient;

function patchEmitter(emitter) {
  var oldEmit = emitter.emit;

  emitter.emit = function() {
    var emitArgs = arguments;

    console.log(emitArgs);

    oldEmit.apply(emitter, arguments);
  }

}


(async function() {

  let db;

  try {

    const client = new MongoClient();

    client.on('serverOpening', () => console.log('connected') );

    db = await client.connect('mongodb://localhost/test', {
      //loggerLevel: 'info'
    });

    //patchEmitter(db.s.topology);

    db.s.topology.on('close', () => console.log('Connection closed') );
    db.s.topology.on('reconnect', () => console.log('Reconnected') );


  } catch(e) {
    console.error(e)
  }

})()

      

So, these two listeners have defined:

    db.s.topology.on('close', () => console.log('Connection closed') );
    db.s.topology.on('reconnect', () => console.log('Reconnected') );

      

They are going to fire when the connection drops and when the reconnection occurs. There are other things as well, such as reconnection attempts, which are also found in the case of the emitter, as you might see with the setting turned on loggerLevel

.

+4


source







All Articles