Sunday, 12 August 2012

FaceBook Real-Time Updates API Tutorial - Part II


"The Graph API supports real-time updates to enable your application using Facebook to subscribe to changes in data in Facebook. Your application can then cache data and receive updates, rather than polling Facebook’s servers. Caching data and using this API can improve the reliability of your application and decrease its load times.




Tutorial
In the first part of the series, we covered how to subscribe to connections and receive notifications from Facebook.

I've seen a lot of people around on forums who have managed to subscribe to connections, but still don't know if they are actually receiving any notifications. The documentation for this API never actually goes as far as telling you what to do with the notifications that you receive from FaceBook.

In this second article, we’ll therefor cover how to decode the incoming JSON objects, and also how to insert these into a database so that they don't just vanish into thin air.

So lets start with setting up our database table!

Setting Up The Table Structure In Our Database:
The CREATE TABLE statement below will create a table called FaceBook with all the needed columns for inserting our data. (This statement should work in any SQL database)


Before you copy paste the code above however, you may need to modify it a little depending on how you intend to use these notifications.

Example:
You have an application with a list containing all the logged in user's friends and you wish to reload this list whenever the list of friends has changed.

In this example, knowing how many times that the friends connection has changed would just be redundant and take up unnecessary space. Having one row telling us that is has changed is enough.

So.. By setting both the uid and changed_fields columns to be primary keys, we tell the database that there can only ever be one row where the uid=123456789 and the changed_fields = 'friends'.

This way, the max number of rows per user in your table will at all times be equivalent to the number of connections that you have subscribed to.

If you for some reason do need to know this, then just remove that last line that's setting the primary keys.

Now that the table structure is done, we can change the script that we created in the first part of this tutorial so that it inserts the values from the JSON object's key-value-pairs into our newly created mysql table.


Inserting The Decoded Object Into Our Database:
Below you will see that I have made some modifications to the script that we use in part I of this tutorial-series. Whenever it now receives a JSON object notification from FaceBook, it connect to our mysql database, decode the contents of the object and then finally insert this information into the database.

Conclusion
Aight, thats it for this second part of this tutorial-series! I hope that this has helped you to understand how to decode the real-time updates that you receive from FaceBook and also how to store them for later use..  

Please feel free to leave a short comment to let me know that this tutorial was of any use to you.

The next and final part of this tutorial-series will cover how to check if there has been a change to the subscribed FaceBook data.  

Documentation
The documentation for the FaceBook Real-Time Updates API can be found at: https://developers.facebook.com/docs/reference/api/realtime/