0

This webpage is kind of a newsboard, and I want it to be updated with the lastest data in my Database, which is delivered through an API, without the need of a page refresh.

The easiest way I thought was with a timer that make a call every X seconds, but I'm looking for a better approach that use less resources. I read about Webhooks but I can't find a way to implement this and get what I want.

Any suggestions will be really helpful. Thanks in advance!

Julián Pera
  • 107
  • 5
  • 12
  • Ajax request is better way because it can reload just an iframe of your site, not all the page. – mastisa Jul 04 '18 at 05:37

3 Answers3

0

Depends on your backend technology. If you're a .NET developer the most well known real-time web functionality library out there is SignalR.

There are lots of other libraries such as socket.io or APIs like pusher.

They are not very difficult to implement, but since they will anyway add another level of complexity to your app, you might need to think more about what to transfer between your publisher/subscriber methods.

In my experience, we used to provide the subscribers with the whole object when a property was changed, but later we decided to just notify all subscribers about the change, and leave it to them to decide if they want to refresh with new data.

  • This is the answer I was looking for. Im gonna do some research about SignalR, because it's what I want to implement. Thank you so much! – Julián Pera Jul 08 '18 at 03:36
0

What I think you're asking is how to continuously get feedback from a source. I'm not sure exactly how your API works, but if the news element your looking for is something simple like an <iframe> it may be possible to check whether there is an update using Javascript like:

var mynewsfeed = ""; //Declares newsfeed variable
setInterval(function(){
     if (API.update != nul) { //Should lighten the load on the processor
         mynewsfeed = API.update() //Updates newsfeed
         document.getElementById("newsfeediframe").src=mynewfeed;
     }
}, 10000); //Will check every 10 seconds (can be shorter)

Sadly, since you haven't provided any code, or what API you're using I can't help much more. If you edit your post it would be easier to help.

Another thing you could try is (if the API supports it) simply doing something like

if (API.checkForUpdate()) {
    //Do something
}
0

Make an ajax call at your desired time interval. Here is your possible answer-Call jQuery Ajax Request Each X Minutes

Chez
  • 13
  • 5
  • The OP said, "The easiest way I thought was with a timer that make a call every X seconds, but I'm looking for a better approach that use less resources." Meaning they don't want this kind of answer. My post may do something similar, but it impliments optimisation. –  Jul 05 '18 at 06:54