Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does facebook push data to news feed?

I am curious as to how Facebook pushes data to the browser as in the news feed. New data shows up at the top of the feed without reloading the page or clicking a button
Does Facebook achieve this by polling their server through AJAX at a set interval or do they somehow push new data from the server to the client unprovoked?
If so what language or API do they use to do this?

like image 558
Drew Galbraith Avatar asked Jul 12 '11 19:07

Drew Galbraith


3 Answers

It's actually called 'long polling', or 'comet'. There are different ways to perform server push but the most common way is to keep connection open while it receives data (it has drawbacks as browser have limit of number of open connections to a host). Facebook had open-sourced the Tornado web servers, which can handle a lot of open connections (which can be a problem is you have a lot of users, but you are using apache for example). In the moment you receive AJAX response, you simply perform a new request, waiting for next response.

like image 174
Maxim Krizhanovsky Avatar answered Nov 15 '22 13:11

Maxim Krizhanovsky


Essentially the code does an AJAX call to their servers, and either waits for a response which triggers another request, polls on a timer, or they open a websocket to receive data as soon as it's pushed. This of course is for "new" data showing up at the top of the feed. When the page is reached at the bottom, they just do another AJAX call to get the next n items.

like image 24
Nathan Loyer Avatar answered Nov 15 '22 13:11

Nathan Loyer


They push it with AJAX, and they use (at least they USED to use), infinite scrolling.

So you'd load your page, and they'd make an initial call to the server to load some messages based on who is logged in, say with a framework like JQuery:

http://api.jquery.com/jQuery.ajax/

And then as you scroll down, they make note of when you're close to the bottom of the page and they need to load more so you're not left without data, and then they make another call automatically. This is called infinite scrolling, and keeps track of where you are in the DOM:

Just one example: http://ajaxian.com/archives/implementing-infinite-scrolling-with-jquery

like image 35
slandau Avatar answered Nov 15 '22 12:11

slandau