AngularJS: Best way to handle data from socket with ng-repeat

Neil

I am quite new to real-time data with Socket.IO. I am trying to figure out what is the best way to handle data coming in from Socket.IO in an AngularJS ng-repeat?

Right now I have the backend sending up the entire data structure every time there is an update to it.

But this is causing the ng-repeat to refresh in order to display the new data (if I make any modifications to the data on the client side only, they are removed next time data is sent from the backend). Is there a better way to do this?

Should I filter out the only data that requires updating on the backend and then send that to the front end instead of delivering the entire data structure again? (But then how would I apply this in an ng-repeat with only the update data...)

I tried using UnderscoreJS extend method in the frontend but it seems to cause issues with the sorting of data in ng-repeat (orderBy and filter both break).

My specific use case is dealing with sports scores (several games (objects) in an array), I am constantly getting updates on my data whether they are the time changing in a game, or a scoring play. Updates are happening pretty much every second. I am tapping into this specific API and grabbing all the data on a specific refresh variable, then sending it to the front-end via a socket. Not the ideal case to use sockets really, but it is the only way to interact with the API that I have discovered so far.

Michael Benford

I think it's best if you send to the client new events only, not the entire list every time. You could add those events to an array and let ng-repeat render them. Here's an example using ngSocketIO, a simple SocketIO module I've created for Angular (the syntax won't be much different than the one you're already using, I guess):

app.controller('MyCtrl', function($scope, socket) {
    $scope.events = [];

    socket.on('someEvent', function(data) {
        $scope.events.push(data);
    }
}

And the markup could be something like this:

<ul>
  <li ng-repeat="event in events">{{ event.name }}</li>
</ul>

Now you can change any object of the events array and it won't be removed when the next event arrives. If the same event can be sent again (with new data, perhaps), then you could replace the array with a "hash":

app.controller('MyCtrl', function($scope, socket) {
    $scope.events = {};

    socket.on('someEvent', function(data) {
        $scope.events[data.id] = data;
    }
}

The ng-repeat expression needs to be slightly changed in order for it to iterate over a hash:

<li ng-repeat="(id, data) in events">{{id}}: {{data.name}}</li>

In this case any local changes to an event will be lost if the same event arrives again. If you want to partially update the local copy of if, you'll need to do it manually, perhaps by saving the parts you don't want to lose and restore them after updating the local event with the new data from the server.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Best way to use ng-repeat in Angularjs for specific field

From Dev

AngularJS : Two way data binding - directive with ng-repeat - not working

From Dev

What is the best way to handle multiple socket connections

From Dev

Fetch data from database and use it in ng-repeat in AngularJS

From Dev

Create Table from JSON Data with angularjs and ng-repeat

From Dev

AngularJS ng-repeat get data from JSON

From Dev

Fetch data from database and use it in ng-repeat in AngularJS

From Dev

How to append data in ng-repeat scope from outside in angularjs?

From Dev

Filter ng-repeat table with data coming from directives Angularjs

From Dev

Yeoman generated angularjs ng-repeat not showing data from controller

From Dev

Angularjs json data name from ng-repeat

From Dev

Updating angularjs ng-repeat with data from firestore

From Dev

Best way to handle disposable data

From Dev

Is there a way to pass data from `ng-repeat` to other element conditionally?

From Dev

What is the best way to handle events in AngularJS in this case?

From Dev

Is there a way to 2-way bind $index from ng-repeat on a model without javascript, using AngularJS?

From Dev

What is the best way to pass data from ASP.NET to AngularJS?

From Dev

AngularJS: ng-repeat with dynamic data

From Dev

Angularjs data-ng-repeat is not working

From Dev

AngularJS scope not binding data in ng-repeat

From Dev

AngularJS ng-repeat not listing data

From Dev

AngularJS: ng-repeat with dynamic data

From Dev

AngularJs - data not showing in ng-repeat

From Dev

AngularJS Data Driven dropdown with ng-repeat

From Dev

Sort AngularJS ng-repeat with dictionary data

From Dev

What is the best practice to show the data fetched by Laravel 4.2 from Database using 'ng-repeat' of AJS

From Java

AngularJS ng-repeat handle empty list case

From Dev

Best way to handle data attributes in Slim

From Dev

Best way to handle large data in c++?

Related Related

  1. 1

    Best way to use ng-repeat in Angularjs for specific field

  2. 2

    AngularJS : Two way data binding - directive with ng-repeat - not working

  3. 3

    What is the best way to handle multiple socket connections

  4. 4

    Fetch data from database and use it in ng-repeat in AngularJS

  5. 5

    Create Table from JSON Data with angularjs and ng-repeat

  6. 6

    AngularJS ng-repeat get data from JSON

  7. 7

    Fetch data from database and use it in ng-repeat in AngularJS

  8. 8

    How to append data in ng-repeat scope from outside in angularjs?

  9. 9

    Filter ng-repeat table with data coming from directives Angularjs

  10. 10

    Yeoman generated angularjs ng-repeat not showing data from controller

  11. 11

    Angularjs json data name from ng-repeat

  12. 12

    Updating angularjs ng-repeat with data from firestore

  13. 13

    Best way to handle disposable data

  14. 14

    Is there a way to pass data from `ng-repeat` to other element conditionally?

  15. 15

    What is the best way to handle events in AngularJS in this case?

  16. 16

    Is there a way to 2-way bind $index from ng-repeat on a model without javascript, using AngularJS?

  17. 17

    What is the best way to pass data from ASP.NET to AngularJS?

  18. 18

    AngularJS: ng-repeat with dynamic data

  19. 19

    Angularjs data-ng-repeat is not working

  20. 20

    AngularJS scope not binding data in ng-repeat

  21. 21

    AngularJS ng-repeat not listing data

  22. 22

    AngularJS: ng-repeat with dynamic data

  23. 23

    AngularJs - data not showing in ng-repeat

  24. 24

    AngularJS Data Driven dropdown with ng-repeat

  25. 25

    Sort AngularJS ng-repeat with dictionary data

  26. 26

    What is the best practice to show the data fetched by Laravel 4.2 from Database using 'ng-repeat' of AJS

  27. 27

    AngularJS ng-repeat handle empty list case

  28. 28

    Best way to handle data attributes in Slim

  29. 29

    Best way to handle large data in c++?

HotTag

Archive