
// This file is the app starting point.
// It launches the server contained in the 'server' variable.

/*jshint node:true, laxcomma:true*/
/*eslint global-require:"warn"*/

'use strict';

// To have docker messages with beautiful colors on console.
var kuler = require('kuler');

var heartbeats = require('heartbeats');

var instances = require('./helpers/instances');

// config : variable containing app configuration elements.
// start : callback function declared a bit later.
module.exports = function(config, start) {

  // App configuration initialisation before web server launch.
  config.set('theme', __dirname);
  config.set('timeout', 1E6);

  // Web server launch.
  // err : to catch errors.
  // If all good, the 'server' variable contains the server.
  start(function online(err, server) {

    // Use socket.io on the server.
    var io = require('socket.io').listen(server);

    var heartAlreadyCreated = false;

    // Caching the instances list to have a reference for comparisons to come.
    var cacheInstances = {};


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

      if(!heartAlreadyCreated) {



        heartAlreadyCreated = true;



        // THE HEARTBEATS HEART

          // Repeat every 5000 milliseconds = every 5 seconds.
          var heart1 = heartbeats.createHeart(5000);

          // For infinite repeat we use {repeat : 0}.
          heart1.createEvent(1, {repeat : 0}, function(heartbeat, last){

            // Instructions done on each heart beat.

              console.log("########## BEAT ! ##########");

              instances.getInstances(function(err,beatInstances){

                // If there are some differences between cacheInstances (reference) and beatInstances (just get) :
                  // Update cacheInstances with beatInstances.
                  // Broadcast to all users this new version to update the 'containers' variable in template.html.
                if(!(JSON.stringify(cacheInstances) === JSON.stringify(beatInstances) )) {

                  cacheInstances = beatInstances;
                  socket.broadcast.emit('refreshInstances', beatInstances);

                }

              });

          });

      }

    });










    if (err instanceof Error) {
      console.error(kuler('Unable to init the server.', 'red'), kuler(err.toString(), 'orangered'));
      process.exit(3);
      return;
    }
    var pack = config.get('package');
    if (pack) {
      console.info(kuler('App detected.', 'olive'), kuler(pack.name
        + ' ' + pack.version, 'limegreen'));
    }
    console.info(kuler('Server is listening.', 'olive'),  kuler(config.get('baseURL')
      + '/', 'limegreen'));

  });
};
@@ -884,24 +884,96 @@
 n(server);%0A%0A
+    // Bool to check if a heart has already been created on the server.%0A
     var hear
@@ -997,16 +997,16 @@
  false;%0A
-
 %0A    // 
@@ -1107,17 +1107,59 @@
  = %7B%7D;%0A%0A
+    // When a user connects to the server.
 %0A
-
     io.s
@@ -1198,24 +1198,117 @@
  (socket)%7B%0A%0A
+      // If no heart already created on the server, we create one and the bool becomes true.%0A
       if(!he
@@ -1329,18 +1329,16 @@
 ted) %7B%0A%0A
-%0A%0A
         
@@ -1366,18 +1366,16 @@
  true;%0A%0A
-%0A%0A
         
@@ -1708,24 +1708,49 @@
             
+// Debug%0A              //
 console.log(
@@ -2376,25 +2376,24 @@
           %7D%0A
-%0A
             
@@ -2394,25 +2394,24 @@
         %7D);%0A
-%0A
           %7D)
@@ -2416,17 +2416,16 @@
 %7D);%0A
-%0A
       %7D%0A
 %0A   
@@ -2420,17 +2420,16 @@
       %7D%0A
-%0A
     %7D);%0A
@@ -2430,24 +2430,16 @@
   %7D);%0A%0A%0A
-%0A%0A%0A%0A%0A%0A%0A%0A
     if (

Test heartbeat on vilodex.