Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

How do certain services, just like google plus and facebook, check for new content without reloading the page? Whenever you are on any of those websites a new notification of a post related content just pops up without the need to refresh the page. Can someone give me some tips on how this works? :)

share|improve this question
1  
Google this: Websocket. ServerSent Events. SignalR –  tunmise fasipe Dec 7 '13 at 0:03
    
Thank you very much :p –  user3050963 Dec 7 '13 at 0:04
add comment

closed as too broad by GlenH7, gnat, Simon, Glenn Nelson, Dynamic Dec 18 '13 at 22:16

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 0 down vote accepted

You use ajax with php. You get the rows from php and use ajax post to get the results. Let me show you: Include this in the page you want to do the reloading.

Link to ajax this will call the ajax version 1.7.2. I got this little code from the internet :( but it will suffice to show you.

<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#divToRefresh').load('/path/to/your/php/file/load.php');
}, 3000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
});
// ]]></script>

And here is the div that will be refreshed

Loading .....

and finally the load.php

//get some content from the database
//I just ordered by time so you can get the latest  items and limit by 10 if they overflow
$sql = "SELECT * FROM `your_table` ORDER BY time DESC LIMIT 10";
//get them or show an error if something went wrong
$rows = mysq_query($sql) or die(mysql_error());
while($r = mysql_fetch_assoc($rows)){
$content = $r['content'];
echo $content;

}
share|improve this answer
    
I see, that's quite interesting! But how does that happen to differ from what tunmise fasipe wrote? Is it more advised, less, more server-exhausting... –  user3050963 Dec 7 '13 at 0:32
    
I am not sure about what he wrote but check out my clients site,I use a similar suggestion link try changing the terms and seee what happens, it wont reload. and refreshes the content –  user1868773 Dec 7 '13 at 0:35
    
interesting... that's really quite nice actually... but suppose I would want to "continuously" (20000ms to 20000ms for example) check for new content, how would that be possible? I'm asking this because I got that your client's website refresh is triggered by that checkmark, and I would like this to happen async and independently to the user behaviour –  user3050963 Dec 7 '13 at 0:41
    
just change the 3000 to 20000 ms. –  user1868773 Dec 7 '13 at 0:43
    
But is "setInterval" ajax'ed? I thought it should run only one time through the code... –  user3050963 Dec 7 '13 at 0:44
show 6 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.