lib/countly-bulk.js
function heartBeat(){
        var isEmpty = true;
        //process event queue
        var eventChanges = false;
        for(var device_id in eventQueue){
            if(eventQueue[device_id].length > 0){
                eventChanges = true;
                if(eventQueue[device_id].length <= conf.max_events){
                    self.add_request({device_id:device_id, events: eventQueue[device_id]});
                    eventQueue[device_id] = [];
                }
                else{
                    var events = eventQueue[device_id].splice(0, conf.max_events);
                    self.add_request({device_id:device_id, events: events});
                }
            }
        }
        if(eventChanges){
            isEmpty = false;
            storeSet("cly_bulk_event", eventQueue);
        }
        
        //process request queue into bulk requests
        if(requestQueue.length > 0){
            isEmpty = false;
            if(requestQueue.length <= conf.bulk_size){
                toBulkRequestQueue({app_key:conf.app_key, requests: JSON.stringify(requestQueue)});
                requestQueue = [];
            }
            else{
                var requests = requestQueue.splice(0, conf.bulk_size);
                toBulkRequestQueue({app_key:conf.app_key, requests: JSON.stringify(requests)});
            }
            storeSet("cly_req_queue", requestQueue);
        }
        
        //process bulk request queue
        if(bulkQueue.length > 0 && readyToProcess && getTimestamp() > failTimeout){
            isEmpty = false;
            readyToProcess = false;
            var params = bulkQueue.shift();
            log("Processing request", params);
            makeRequest(params, function(err, params){
                log("Request Finished", params, err);
                if(err){
                    bulkQueue.unshift(params);
                    failTimeout = getTimestamp() + conf.fail_timeout;
                }
                storeSet("cly_bulk_queue", bulkQueue);
                readyToProcess = true;
            });
        }
        
        if(isEmpty){
            empty_count++;
            if(empty_count === 3){
                empty_count = 0;
                if(empty_queue_callback){
                    empty_queue_callback();
                }
            }
        }
        
        if(initiated){
            setTimeout(heartBeat, conf.interval);
        }
    }
function heartBeat(){
        var isEmpty = true;
        //process event queue
        var eventChanges = false;
        for(var device_id in eventQueue){
            if(eventQueue[device_id].length > 0){
                eventChanges = true;
                if(eventQueue[device_id].length <= conf.max_events){
                    self.add_request({device_id:device_id, events: eventQueue[device_id]});
                    eventQueue[device_id] = [];
                }
                else{
                    var events = eventQueue[device_id].splice(0, conf.max_events);
                    self.add_request({device_id:device_id, events: events});
                }
            }
        }
        if(eventChanges){
            isEmpty = false;
            storeSet("cly_bulk_event", eventQueue);
        }
        
        //process request queue into bulk requests
        if(requestQueue.length > 0){
            isEmpty = false;
            if(requestQueue.length <= conf.bulk_size){
                toBulkRequestQueue({app_key:conf.app_key, requests: JSON.stringify(requestQueue)});
                requestQueue = [];
            }
            else{
                var requests = requestQueue.splice(0, conf.bulk_size);
                toBulkRequestQueue({app_key:conf.app_key, requests: JSON.stringify(requests)});
            }
            storeSet("cly_req_queue", requestQueue);
        }
        
        //process bulk request queue
        if(bulkQueue.length > 0 && readyToProcess && getTimestamp() > failTimeout){
            isEmpty = false;
            readyToProcess = false;
            var params = bulkQueue.shift();
            log("Processing request", params);
            makeRequest(params, function(err, params){
                log("Request Finished", params, err);
                if(err){
                    bulkQueue.unshift(params);
                    failTimeout = getTimestamp() + conf.fail_timeout;
                }
                storeSet("cly_bulk_queue", bulkQueue);
                readyToProcess = true;
            });
        }
        
        if(isEmpty){
            empty_count++;
            if(empty_count === 3){
                empty_count = 0;
                if(empty_queue_callback){
                    empty_queue_callback();
                }
            }
        }
        
        if(initiated){
            setTimeout(heartBeat, conf.interval);
        }
    }
heart beat