"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
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.
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.
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.
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.
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/
Thanks for the tutorial! Really nice and clear explanation.
ReplyDeleteFrom your code,
ReplyDeleteI made out a simple test to see if things are working :
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' && $_GET['hub_verify_token'] == VERIFY_TOKEN) {
echo $_GET['hub_challenge'];
} else if ($method == 'POST') {
$post_body = file_get_contents("php://input");
$object = json_decode($post_body, true);
$file = fopen("write.txt","a+");
$fwrite = fwrite($file,$method);
}
And I dont think I am getting any API notifications at all. My subscriptions for 'friends' is active.
Sorry for the delay... Does it output anything if you write $object to file?
Deletefile_put_contents('/filepath/updates.txt',$object, FILE_APPEND);
Ok i have done all this successfully. The data that comes over in the changefield doesnt actually contain the update? it just lets us know that the data has been updated. The question is been updated to what? How do we retrieve the actual updated information after you are notified?
ReplyDeleteIn my case i want to receive this data while the user is not even logged in. I would like to retrieve the users feed to display within my site without the user actually being logged in.
I was told Realupdates would do the trick.
This API just lets you know that there has been changes in for example feed and friends for userid 1234567. You will have to use the Facebook Graph API to pull the actual changes in data.
DeleteNice tutorial and waiting for part III
ReplyDeletevery good work , to wait the part third !!!
ReplyDeleteWhat should be the "filepath" in :
ReplyDeletefile_put_contents('/filepath/updates.txt',$object, FILE_APPEND);
Is it the filepath to my server? or Facebook's?
The file path to your text file on your server.
DeleteHi, I also have been trying out with realtime api. I have created an endpoint which simply logs the request when I get a post request. But I don't seem to be getting any calls from facebook. If I manually do a post to the end point, it logs the post body json.
ReplyDeleteThis is the response I get for the app subscription
{
"data": [
{
"object": "user",
"callback_url": "https://myappname.herokuapp.com/realtime.php",
"fields": [
"activities",
"feed",
"is_app_user",
"likes",
"online_presence",
"photos",
"picture",
"status",
"wall_count"
],
"active": true
}
]
}
I made the callback test which returned null only.
I also made a few status updates and likes as public but I still not able to receive any post requests. I've checked my heroku logs, there is no sign of a post request from fb. can you tell me if I'm missing out on something. Thanks
*realtime.php*
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe') {
echo $_GET['hub_challenge'];
} else if ($method == 'POST') {
$data = file_get_contents("php://input");
$json = json_decode($data, true);
error_log("update recieved");
error_log('updates = ' . print_r(json, true));
}
?>
Hello AlexanderNorway, i found your tutorial very useful. and i am getting my updates and i have created one json file and i am appending response in that json. it works successfully.
ReplyDeleteThanks alot. but my next problem is that i have subscribed for two objects
1 Page (field = Feed)
2 User (field = Feed)
i am getting updates from both objects.
In case of Page
face book sending me updates me for each activity like,post,status,comment. means for each activity one response.
but in case of user i am getting response frequently, even if i have not updated anything. still i am getting response.
{"object":"user","entry":[{"uid":"100001316052445","id":"100001316052445","time":1374040661,"changed_fields":["feed"]}]}
{"object":"user","entry":[{"uid":"100001316052445","id":"100001316052445","time":1374041543,"changed_fields":["feed"]}]}
{"object":"user","entry":[{"uid":"100001316052445","id":"100001316052445","time":1374041548,"changed_fields":["feed"]}]}
{"object":"user","entry":[{"uid":"100001316052445","id":"100001316052445","time":1374041561,"changed_fields":["feed"]}]}
{"object":"user","entry":[{"uid":"100001316052445","id":"100001316052445","time":1374041612,"changed_fields":["feed"]}]}
{"object":"user","entry":[{"uid":"100001316052445","id":"100001316052445","time":1374041627,"changed_fields":["feed"]}]}
how can i track exact changes. i mean for which activity i am getting particular response. i am confused and cant able to proceed. Please help me.. Thanks in andvance.
did you resolve this problem ? Or anyone solved this ? Help please. Thank you
DeleteThis comment has been removed by the author.
ReplyDeleteJust wanted to say thanks for the 2 tutorials. They helped me allot.
ReplyDeleteCheers
Rob
Please help me. How can I get notification for a fb page? What should I do.
ReplyDeletePlease help. I am desperate
Here you go:
ReplyDeletehttp://tech-brains.blogspot.in/2015/04/real-time-updates-for-facebook-page.html
Hope this helps!