I have a websocket connection that pulls json data in angularjs. The basic idea is code:5 items in the array are questions and code:6 items in the array are requests to delete the question.
the first jSON data received would look like this:
[{"code":1,"timestamp":"2017-07-11T21:00:16.655706556Z","text":"Welcome to the listening pool, total clients 1","uuid":"51c7d6c7-8b6a-4b39-a460-343480e6cd23"},{"code":5,"timestamp":"2017-07-11T21:00:18.736695286Z","text":"By trying we can easily learn to endure adversity. Another man's, I mean.\n\t\t-- Mark Twain\n","uuid":"9046a275-ada8-415a-9dd3-37bdbfc43f75"}]
the code:5 item would then be posted to the page in a question queue. The second update from the websocket would include a code:6 to remove the already posted question (code:5). I am using the uuid value to find the matches and remove the question.
[{"code":1,"timestamp":"2017-07-11T21:00:16.655706556Z","text":"Welcome to the listening pool, total clients 1","uuid":"51c7d6c7-8b6a-4b39-a460-343480e6cd23"},{"code":5,"timestamp":"2017-07-11T21:00:18.736695286Z","text":"By trying we can easily learn to endure adversity. Another man's, I mean.\n\t\t-- Mark Twain\n","uuid":"9046a275-ada8-415a-9dd3-37bdbfc43f75"},{"code":6,"timestamp":"2017-07-11T21:00:23.870193214Z","uuid":"9046a275-ada8-415a-9dd3-37bdbfc43f75"}]
The problem I'm running into is that it only removes the first duplicate . When more data is received from the web socket it puts the old data already removed items back in.
Any help would be greatly appreciated.
This is my angular code:
<section ng-controller="WaitingRoom">
<ul ng-repeat="data in MyData.collection | orderBy: '-timestamp' track by $index" ng-show="data.code == 5 && data.uuid != MyData.collection[$index].uuid">
<li id="code"> Code: {{ data.code}} </li>
<li> Timestamp: {{ data.timestamp | date:'medium'}} </li>
<li> Question: {{ data.text}} </li>
<li id="uuid"> ID: {{ data.uuid}} </li>
</ul>
</section>
angular.module('MY_APP', ['ngWebSocket'])
//
.factory('MyData', function($websocket) {
// Open a WebSocket connection
var dataStream = $websocket('ws://website.com/data');
var collection = [];
dataStream.onMessage(function(message) {
collection.push(JSON.parse(message.data));
});
var methods = {
collection: collection,
get: function() {
dataStream.send(JSON.stringify({ action: 'get' }));
}
};
return methods;
})
.controller('WaitingRoom', function ($scope, MyData) {
$scope.MyData = MyData;
});