Integrating Facebook Comments
This is a post about Facebook comments and counts integration into a Jekyll blog
Integrating the comment boxes
This one is fairly easy. Just register an app and grab a code from the Facebook Comments site, and paste the code to a suitable part of your page. There is a data-href parameter which has a sensible default, although it is best to set it manually. It might come handy when you are migrating to a new domain or reordering your structure, it also makes sure you see the comments in localhost.
Integrating the comment counts
This part, things are getting a little interesting. We need a bit of Javascript to get back the comments count for a given post. We can use this URL:
https://graph.facebook.com/v2.2/?fields=share{comment_count}&id=<URL>
A simple GET request gives back the comments count nested in a JSON object. No OAuth, no access keys needed, just a plain old XHR. Lets see a little example:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://graph.facebook.com/v2.2/?fields=share{comment_count}&id=' + encodeURIComponent(currentPostUrl), true);
xhr.responseType = 'json';
xhr.onload = function (e) {
if (this.status == 200) {
var commentsCount=this.response.share.comment_count;
// Do something with the comment count
}
};
xhr.send();
Graph API limits
Since we are using the Graph API here, we should take a look at the limits. There are several layers of limiting, and most are not detailed in the official site, the closest is 600 calls per 600 seconds, per token & per IP.
In this case, there is no application, since we did not include our appid. Also we do not use client access tokens, partly because we didn't have one (you can if you ise Facebook login). So the effective limiting here is per IP, so it scales with the number of visitors. It is easy to test too: Just make a for-cycle to reach limiting, switch IP, and try again. With the new IP, the requests will be successful.
In conclusion, we should not fear running out of limiting using this method of getting the counts.
Integrating the comment counts sum
This is the part where things are getting interesting, but this is covered in a follow up article.