I am not hoping for fully working examples, I am just in need for a direction to start.
So to explain what I am trying to do, I have an app where users log in, log out, add content, do actions in the site etc. I want to know if it is possible using PHP and AJAX to make a widget that auto updates like this:
I just want a guideline or direction to follow, how would I make real-time widget that auto updates and pull data/actions/etc.. would it be time interval update?
Any ideas?
First of all you need to store a list of actions that you want displayed in the widget, for example you can create a database table with (id, created_at, action_description) and whenever a user performs an action that should be displayed in the widget (e.g uploaded a photo, etc), insert a record in that table (note this is not very efficient and does not scale well if you have many users) Second, on the browser side, you periodically call a function (with setInterval for example) to query the server for updates and update the widget
The above solution is very simplistic and wont scale. Hope that helps
You can use the setTimeout()
javascript method to trigger a reload of your widget every X milliseconds.
var delay = 5000; // 5 seconds
setTimeout('refresh();', delay );
function refresh(){
doAjaxCall(); // ajax call to get new data and update the widget
setTimeout('refresh();', delay ); // schedule next refresh
}
It might be better to put the reschedule (the last line) inside the ajax callback to start the timer only when the current refresh is finished.