Facebook Realtime Update

Prabu D

2 min read

We recently implemented Facebook’s Real Time Page updates and I would like to share my experience on using the Facebook Graph API.

The Graph API has a feature called ‘Real Time Updates’ that enables apps to subscribe to changes in certain pieces of data. When a change occurs, an HTTP ‘POST’ request will be sent to a callback URL belonging to that app. This makes apps more efficient, as they know exactly when a change has happened, and don’t need to rely on continuous or even periodic Graph API requests when changes aren’t happening.
Note The real-time updates only indicate that a particular field has changed, it do not include the value of those fields. It should be used only to indicate when a new Graph API request to that field needs to be made.
Here’s how you can integrate Real Time Updates within your app:
1. Create a Facebook app for your page and get your APP ID (@app_id).
facebook_app
2. To get your access token access the following URL (@user_access_token).
[source]
https://graph.facebook.com/oauth/access_token?client_id=&client_secret=&type=client_cred
[/source]
Note : To get app access token , It’s combination of App ID + %7C + App Secret (@app_token)
3. Use /{app-id}/subscriptions
Using Facebook subscription API the basic operations are, you can create or update the existing subscriptions and you can list each of your existing subscriptions and you can delete subscriptions.
The subscriptions available on user and page Graph API objects.
Example Ruby code:
Here I activated “page” object subscription.
[source]
url = “https://graph.facebook.com/v2.1/#{@app_id}/subscriptions?access_token=#{@app_token}”
@@callback_url = “http://www.spritle.com/realtime/updates”
begin
encoded_url = URI.encode(url)
p encoded_url
rest_client = RestClient.post(encoded_url, {:object =>”page”, :callback_url => @@callback_url, :fields => “feed, name, description, mission, location, awards, picture”, :verify_token => “stringToken”}, :content_type=> “application/json”)
rescue Exception => e
p e.inspect
end
if rest_client
p ” page subcription success”
else
p “page subcription Error”
end
[/source]
To check subscription created successful or not, Go to Facebook Explorer ( Refer the below screen shot ).

the below screen shot shows real-time update objects and fields that this app has subscribed.

subscription
Facebook Verification Requests

When a subscription is successfully created , or modify an existing one,
Facebook servers will make a GET request to your callback URL in order to verify the validity of the callback server. A query string will be appended to this URL with the following parameters:

  • hub.mode – The string “subscribe” is passed in this parameter
  • hub.challenge – A random string
  • hub.verify_token – The verify_token value you specified when you created the subscription

4. Add Page tabs
The apps that this Page has added as tabs. This is useful if you are subscribing to real-time updates from the Page.

A page access token is required to add tabs to that Page.

To get page access token you can get it in Facebook Explorer ( @page_token )

GET /{user-id}/accounts

(Please refer the below screen shot)
page_token
Example ruby code. Here I connected my own page with subscripted app.
[source]
@page_id = “212419718797825”
@page_token = “” #required
url = “https://graph.facebook.com/v2.1/#{@page_id}/tabs?access_token=#{@page_token}”
begin
encoded_url = URI.encode(url)
rest_client = RestClient.post(encoded_url, {:app_id => @app_id}, :content_type=> “application/json”)
rescue Exception => e
p e.inspect
end
if rest_client
p “Add tab success for app_id #{@app_id}”
else
p “Errr”
end
[/source]
5. You can Unsubscribe realtime updates from a page
Example ruby code.
[source]
@page_id = “212419718797825”
@page_token = “” #required
url = “https://graph.facebook.com/v2.1/#{@page_id}/tabs/app_#{@@app_id}?access_token=#{@page_token}”
begin
encoded_url = URI.encode(url)
p encoded_url
rest_client = RestClient.delete(encoded_url, :content_type=> “application/json”)
rescue Exception => e
p e.inspect
end
if rest_client
p “removed tab success for app_id #{@@app_id}”
else
p “Errror”
end
[/source]
see the link – http://developers.facebook.com/bugs/503381706394259
You can check the status of active page subscriptions, using the Graph /tabs GET API call
page_tab
That’s it.
Thanks
happy coding 🙂

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *