How to know that FB API call is finished? - facebook

I am in a bit problem right now. The scenario is, i take pageids of which i am an admin of using FQL. After getting the page ids, i want to to loop through those ids and call the below function:
FB.api('/'+id+'/tabs?fields=image_url,name,application', function(response) {
// Other stuff here
});
And from this i take the tab ids of every page and other details.
The issue is i dont get to know if the requests for every page id to get tabs is over. I have to manually set something like this:
setTimeout(loadAllNow, 5000);
assuming that the request for all pages is done in the time i waited before calling next function.
Is there any way i can get to know if the loop is over with all API calls returned me the desired result. Because while calling the api in a for loop, it just send all the API calls almost together and dont wait for the previous to get over.
Thanks

For this, you can check manually like this-
pagesCount = NO_OF_PAGES;
FB.api('/'+id+'/tabs?fields=image_url,name,application', function(response) {
count++; // initialize this count to 0 before starting the loop
if(count == pagesCount) // this is the final response
{
// Other stuff here
}
});

Related

Is it possible to execute a long running function before the browser is reloaded?

I do prevent a page reload in my web application by the following function:
window.onbeforeunload = (event) => {
const e = event || window.event;
// Cancel the event
e.preventDefault();
save_user_data_to_indexed_db();
if (e) {
e.returnValue = ''; // Legacy method for cross browser support
}
return ''; // Legacy method for cross browser support
};
However, the save_user_data_to_indexed_db() function is not being executed during the "Reload site?" message. I thought that if I could execute my function during the displayed message, I could maybe automatically answer the same dialog programmatically and let the browser continue reloading the page.
Is there a way to make the browser wait for this kind of operation?
Generally, there is no way to make the browser wait. What I often do in this case is write the data to an intermediate place, such as localStorage, synchronously, and then asynchronously copy that data over to indexedDB later on when there is time, such as when the page is next loaded again, or from within a service worker.

What does the comment mean when it says "Do something with the page variable"?

I'm trying to implement a pagination plugin for my webapp, and I found a good one. But I don't understand one section of the code that's commented "Do something with the page variable". I believe this is what's preventing the pagination plugin from completely working for me.
//Pagination
$('.pagination').jqPagination({
paged: function(page) {
// do something with the page variable
}
});
I'm the plugin creator.
What that means is within the paged callback the plugin will pass in the page variable, and within this callback you can now add whatever code you need to achieve your desired output.
If you use the following, it'll simply output the page number in the console:
$('.pagination').jqPagination({
paged: function(page) {
console.log(page);
}
});
Of course, you'll likely want to do more than this, perhaps you'll want to perform an AJAX request to fetch some content:
$('.pagination').jqPagination({
paged: function(page) {
fetch('http://example.com/movies.json?page=' . page)
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
}
});
In short, you'll need to add your own code to the paged callback to do what you want it to do.

Graph API: event's RSVP check

I'm currently trying to get an idea of getting Facebook's RSVP for an event, but really stuck on that part:
as I see now, to get user's RSVP I have to make three requests with the following logic:
request eventID/attending/userID ->
if "data" array count == 0 ->
request eventID/maybe/userID ->
if "data" array count == 0 ->
request eventID/declined/userID
else -> means user didn't make any choice previously.
So here it looks like I have to make 3 requests to facebook's graph api to get users's RSVP for a single event.
The question is if there is any way to get an RSVP status for an event doing a single request?
I'm using the latest Facebook SDK and the latest graph api.
Many thanks in advance.
So the best solution here so far is:
one request to /eventID/attending/userID
one request to /eventID/maybe/userID
one request to /eventID/declined/userID (if needed to know if the event invitation was declined)
Call to eventID/attending(maybe/declined)/userID lets us filter rsvp to single user, so we avoid downloading and processing a large amount of data here.
After call you have two options:
If result is true, you get the following response:
{
"data": [
{
"name": "Oleskii Poplavlen",
"id": "10204715567996406",
"rsvp_status": "attending"
}
]}
If result is false, you get the following response:
{
"data": []
}
So while you still have to make multiple requests to get user's RSVP to event, you can avoid downloading loads of other users's rsvps.
Hope that helps someone!
I can get you started to do it in 2 calls:
FB.api('/'+ event_id + '?fields=id,attending{rsvp_status},maybe{rsvp_status}', function(event_response){
// here you should make a call to check if the user has the event and if so get the rsvp_status
FB.api('/'+ user_id + '/events?fields=id,rsvp_status', function(user_response){
// check if user has event, then log the rsvp_status
if(user_response.id == event_response.id) {
console.log(user_response.rsvp_status);
} else {
console.log("user is not going");
});
});
I have not tested this but I've been playing around with this API for quite a while now. This could be an answer to do it in 2 calls, not in 1 unfortunatly.
The first call doesn't need the 'attending' and 'maybe' fields but it's usefull to test with.

can a fb user send an apprequest to all friends same day no matter how many?

Hi am using this function that works by a single ID (52000121) and comma separatted ID's (5000000,500002,500004,000002001)
function invite(id) {
FB.ui({method: 'apprequests',
message: '<?=get_texto_clave('wants_you_to_join')?>',
title: '<?=get_texto_clave('send_app_request')?> <?=$_SESSION['alias']?>',
to: id,
}, function(response){
if(response!=null){
var invitados = id.split(',');
for( i=0; i<invitados.length; i++){
$('#friend_'+invitados[i]).addClass('invited');
$('#friend_'+invitados[i]+' .inviteButton').addClass('invitedBtn').text('<?=get_texto_clave('Invitado')?>');
}
}
});
}
Wich seems to be working fine,
But I have just sent more than 500 invites to friends of mine, and they all got the 'ivited' class (When i completed the popup);
¿Did they all recived it?
I know the response debugging could be better, but still would like to know if user can send an invite to all of his friends same day no matter how many friends are. Or is there any user/app limitation?
Thanks
There is a no limit to the app requests you send
But if you use multi friend selector, you can select maximum of 50 and 25 in IE.
Naturally, if you're being spammy, you'll still get reported a lot and automated systems could block you, so remember to keep to a reasonable volume of requests.
Btw, You can read the response to know to whom it was delivered.

node.js and socket.io: different connections for different "sessions"

I've got a node.js application that 'streams' tweets to users. At the moment, it just searches Twitter for a hard-coded string, but I'd like to allow users to configure this in the URL (eg. by visiting /?q=stackoverflow).
At the moment, my code looks a bit like this:
app.get('/', function (req, res) {
// page rendering skipped
io.sockets.on('connection', function (socket) {
twit.stream('user', {track: 'stackoverflow'}, function(stream) {
stream.on('data', function (data) {
socket.volatile.emit('tweet', data);
}
});
});
});
The question is, how do I make it so that each user can see a different stream of tweets simultaneously? At the moment, it works fine in a single browser tab, but it falls over as soon as a second one is opened - and the error is fairly deep down inside socket.io. Am I misusing it?
I haven't fully got my head around socket.io yet, so that could be the issue.
Thanks in advance!
Every time a new request comes in, you are redefining the connection callback with io.sockets.on - you should move that block of code outside of app.get, after your initialization statement of the io object.