How does facebook push data to news feed?
Asked Answered
S

3

11

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?

Syman answered 12/7, 2011 at 19:26 Comment(1)
If you have firebug take a look in the console. What you will see it a GET request, likely through an AJAX request, that just spins indefinitely. They real question would be how they keep this request open, as it does not seem to be set on a timeout or interval.Middleton
L
4

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.

Lindsey answered 12/7, 2011 at 20:21 Comment(0)
S
3

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.

Selfpreservation answered 12/7, 2011 at 19:31 Comment(0)
D
2

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

Druse answered 12/7, 2011 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.