I am creating a simple website which reads messages from a facebook page using facebook javascript sdk and facebook API. I need to continuously read the messages as they arrive in the facebook page?. How do I do this?. If I run it continuously in a loop like while(true){ FB.api()}, the website hangs!. I need to poll the messages from the chat continuously and then display it on the website. Any leads on doing that?. Here's what I'm doing. Or I should be able to continuously poll unread messages and then display it to the user!.
FB.api(
"/<pageId>?fields=access_token",
function (response) {
if (response && !response.error) {
console.log(response);
/* handle the result */
document.getElementById('status').innerHTML = response.access_token;
FB.api(
"/<pageid>?fields=conversations{message_count,unread_count}",
{access_token : response.access_token},
function(response2){
if(response2 && !response2.error){
var conversationId = response2.conversations.data[0].id;
var unreadCounter = response2.conversations.data[0].unread_count;
if(!unreadCounter){
console.log("There are no unread msgs currently":unreadCounter);
}
//console.log(conversationId);
FB.api(
"/"+conversationId+"?fields=snippet",
{access_token : response.access_token},
function (response) {
if (response && !response.error) {
/* handle the result */
console.log(response);
}
else{
console.log(response);
}
}
);
//handling result
console.log(response2);
document.getElementById('status').innerHTML = response2.conversations;
}
else{
console.log(response2);
}
}
);
}
else{
console.log(response);
}
},{scope:"read_page_mailboxes"});