0

I want to load data from server via ajax only if data is changed on server. I'm using AngularJS and I need to implement this feature there.

So, for example, certain User has added/edited/deleted some item from application and this item was saved to db. Other users should get refreshed list of items.

I can add something like setInterval, for loading items from server, but this approach seems overkill.

I also can use something that can be implemented by Observer pattern. So I will be need to have the extra column in my db, that will set to true, if update was happened. And next I still need setInterval for load all data, that has updated flag.

Please, show me the best way for loading data via ajax only if data was changed on server and save me from inventing the wheel. (Maybe AngularJS has similar functionality?).

Any help will be appreciated!

4 Answers 4

2

As @xbirkettx suggested, you can try use angular.js together with socket.io.

Example:

app.factory('socket', function ($rootScope) {
  var socket = io.connect();
  return {
    on: function (eventName, callback) {
      socket.on(eventName, function () {  
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(socket, args);
        });
      });
    },
    emit: function (eventName, data, callback) {
      socket.emit(eventName, data, function () {
        var args = arguments;
        $rootScope.$apply(function () {
          if (callback) {
            callback.apply(socket, args);
          }
        });
      })
    }
  };
});

And then from server side emit events, and update your angular model in reaction on particular events.

You will be notified when change has occurred, without page refreshing.

Sign up to request clarification or add additional context in comments.

Comments

2

You cannot do anything with Angular in particular. You can use setInterval but it might overload your server with asynchronous requests.

I have a similar problem and I ended up using WebSockets. Namely Socket.io, (however be aware that depending on your backend it might not be compatible.)

Then on the Socket.io side, I have a publisher subscriber with my result backend (Redis).

Comments

1

AJAX, as the name implies, works aynchronously. The server only responds if the client sends a request. Since the client can't know what happens on the server, the setInterval approach is an acceptable way to solve this (in AngularJS, or any other client environment).

You apparently want your communication to work the other way 'round. AJAX is not your tool of choice then. Websockets may bring you what you want, but if you think setInterval is already overkill...

[Posting as an answer for lack of rep.]

Comments

0

You could keep a timestamp of the last time you retrieved data. Next time you request, send the timestamp to the server, and only respond back records who have been modified since. You'll need a last_modified timestamp column on each table. Your where clauses would query on that field. select * from table1 where ... and last_modified >= timestamp_from_client

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.