Search code examples
facebookpersist

Facebook should I keep a user's friends list in my own database?


I'm developing an app (Flash/AS3) that connects to Facebook. After the user logs in I need to get his friends list (IDs only), check if any of them is a user of my app (through a registry on a database), update the list by adding a flag to every user that is also an app user and then do stuff with the updated list. My question is:

  • should I ask for the friends list, perform the check and retrieve the data to Flash when the app starts? I would have to wait for the response from Facebook plus the time to execute the script (php); I would only have one table with the app's users info;

or

  • should I keep all of the user's friends in my own database and have a cron job automatically updating (maybe once every day) my database, that is, asking for each of my app users' friends list, perform the check, remove duplicates, etc etc ? In this case I would have a table with the app's users, a table with all my users' friends (which could be thousands, of course) and a user_friends table to relate both previous tables;

notes: - I'm using PHP/cURL to make requests to the Graph API - MySQl Database - I will probably also be checking for Twitter and G+ friends for every user (so the script could take even more time, although I could go through each network in sepparate steps)

What do you think is the best approach? I don't mind having a "loading" period in the application. At this point, I already have the first approach almost completed but a colleague suggested the second one so that the app user wouldn't have to wait for Facebook/Twitter/G+/script response. But... isn't that a waste of resources? I mean, Facebook, etc already have the users/friends connections...would I gain anything by replicating that in my own DB and having cron jobs doing the updates hidden from the app users?

Hope I made myself clear! Thanks!


Solution

  • I once faced a similiar problem. You won't be able to update your database routinely because you normally need an active user (=access token) to retrieve that user's friend list. Formerly, you could ask for a permission "offline_access", but this permission is deprecated: https://developers.facebook.com/roadmap/offline-access-removal/

    I decided to write all the friends' IDs into a textfile (User-ID as filename: 123456789.txt) on my server during the first start of my app (showing a spinner and a text saying "loading friends..."). Whenever the app was started afterwards, I pulled the IDs out of that file.

    You can trigger an update of that file by checking it's creation date. If it's "too old", delete and recreate it after you've read all the friend IDs. That way, the file cache will be created in background and not be noticed by the user.