0

I am trying to make a newsfeed for a webpage. Though the only problem I have encountered is refreshing the posts. How can I refresh the newsfeed only when new data has been inserted to mysql database? As you can see, pagerefresh() is being called every 5 seconds but thats inefficient because I am printing the data over and over again. Also when a user is trying to comment on the newsfeed, the posts keeps getting refreshed thus interrupting the user from his typing. I haven't included all the code, but my main question is how can you refresh only when new data has been entered?

header.php

function initializePosts()
{

$.post("searchPresentation.php", {refresh_posts:"yes", user: "<?php echo $user ?>"},    function ( data ){
    $("#profile_newsfeed2").html(data);
});

setTimeout("initializePosts();", 5000);
}

index.php

<script>
initializePosts();
</script>

<div id="profile_newsfeed2"> </div>
  • That's called pushing to client: http://stackoverflow.com/questions/5753674/php-best-way-to-push-data-from-server-to-clients but with PHP you probably ought to just stick with polling. – developerwjk Jun 24 '14 at 22:59
  • study up on websockets and hire a professional to build you a websocket server(or write your own with something like ratchet for php). you can use said server to push data to your clients, or instead you can send a command to trigger an ajax event. so: read up on serverside websocket technology and the javascript websocket api. it is much easier than ajax once you get your legs, and you will never want to go back to ajax. – r3wt Jun 24 '14 at 23:16

0 Answers0