I'm trying to create a real-time sync system that constantly pulls new chat 
messages from the database every time its added for all devices. I added a 
column named 'isNew' at the end of all of my chats table's structure that I 
used to pull new messages from the database using the $interval function 
over every half-second to get new chats and then concatenate it onto the 
list of messages. Below is the code I have to accomplish this.

    $scope.insert = function(chat){
        $http.post('php/adb/insert.php', {'query': 'INSERT INTO `chats` 
(message, createdAt, isNew) VALUES ("'+chat.message+'", NOW(), 1)'}).success
(function(data){
            console.log(data);
        });
    };
    $interval(function(){
        _uno.get('get chats isNew 1 spec', true).success(function(data){
           for(var i = 0; i < data.length; i++){
               $scope.chats = $scope.chats.concat(data[i]);
               console.log(data[i]);
               $http.post('php/adb/update.php', {'query': 'UPDATE `chats` 
SET `isNew` = 0 WHERE `id`='+data[i].id+''}).success(function(_data){
                    //console.log(_data);
               });
            }
        });
    }, 500);


The 'get chats isNew 1 spec' string and '_uno' function is a custom 
database querying template I created. It simply means "SELECT * FROM chats 
WHERE isNew = 1". The code works but when I try to test it for all devices, 
sometimes one device doesn't update while another does, and sometimes it 
doesnt update at all but it inserts the chat into the database just fine 
but doesn't render it onscreen.. Is there anything I'm missing or need to 
re-write?



-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to