I want to use jQuery to consume an RSS feed, and then create some HTML content that will define the items within the RSS feed. This is easy enough for me to do. However, I also want my page to automatically update should the source that is supplying the RSS feed change.
For example, assume that the intial page load consumes the RSS feed and it contains two items: { George Washington and John Adams }
The page then creates two HTML elements (anchor tags, divs, whatever) that display “George Washington” and “John Adams”.
Thirty seconds later the source that supplies the RSS feed now contains three items: { George Washington, John Adams and Thomas Jefferson }. How do I go about setting my page up so that it knows the RSS feed has changed and that it needs to refresh/recreate the HTML elements to reflect this change?
The most obvious answer is to use window.setInterval and have the page check the RSS feed at given intervals to see if it has changed. But I feel like I’m missing something here and that there is a better way to do it.
Any ideas would be greatly appreciated. My ignorance is astounding when it comes to the RSS/Atom feed world, so don’t assume that I already know it (because I most likely don’t!) and give me your help!
Unless you can somehow get a callback when the feed is updated, which the feed owner would need to provide, and which would require a server-side solution like Node.js to put the callback through to the client, the
setIntervalway is the only way.What you can do, to avoid loading the whole feed every check, you can first do a
HEADrequest and check if theContent-Lengthwas any different than the last request. Keep in mind this assumes a few things, like thatContent-Lengthwill be returned and that it will always increase (if the feed only returns something like the last 10 elements, then theoretically, although unlikely, you could get the sameContent-Lengtheven if there was a new article).