I was curious how facebook can show content live. For example, when one user comments, another person from somewhere else in the world can see that comment appear right after the user comments.
I know ajax/jquery can do an .append(data) without refreshing the page, but that won’t show on other users. Would it?
I’m not sure exactly how Facebook does it, but heres how it probably works.
See https://www.facebook.com/notes/facebook-engineering/inside-facebook-messages-application-server/10150162742108920
EDIT:
To go in deeply with what function you should use.
Firstly you have a list of comments.
I set the list-style-type to none so the bullets would not show.
When the page loads, you use a jquery AJAX function, to perform a HTTP GET Request to the page serving comments. You then inject them into the DOM, using
Where loadedAJAXData is the data you got from a GET request.
So the injected data looked like this –
and now the comments list looks like this –
So you have your comments now. Someone makes a new comment? It is sent to your website, which inserts a new comments row into the database.
Say you have another page, which you request the number of comments from. Lets call it comments.php?count.
For example, in this script, the browser is requesting comment count every 2 seconds. Our oldCommmentCount was 0. Our newCommentCount is 2. For each new comment, we will make a request to comments.php?get=commentOffset. Where commentOffset is i. Requesting the new comment, we get its data, append it to the comments list, and then increment oldCommentCount.