Selesai!
-
Setel server.socketoptions.keepAlive ke 1 . Cukup perbarui objek opsi seperti ini:
options = { auto_reconnect: true, db: { w: 1 }, server: { socketOptions: { keepAlive: 1 } } };
-
Ping database secara berkala. Berikut cuplikan kode yang melakukan hal itu:
printEventCount = function() { db.collection("IOSEvents").count(function(err, numberOfEvents) { console.log(new Date() + ": error = " + err + ", number of events = " + numberOfEvents); ping(); }); }; ping = function() { if (config.pingPeriod === 0) return; setTimeout(printEventCount, config.pingPeriod); };