Using Facebook Realtime Updates in Ruby on Rails

Rajeswari K

2 min read

Recently I got chance to try Facebook real time API in one of our project. Realtime Updates enables applications to subscribe to changes in data. For Facebook apps, instead of continuously polling the Facebook Graph API for updated data we can get update only when the data is changed in Facebook.

Whenever a subscribed change occurs, Facebook makes a HTTP POST request to a callback URL that we specify by sending list of changes happened. Facebook app will generally receive notification of the change within a couple of minutes of its occurrence.
We can get updates for following objects with certain fields in it:

  • User
  • Page
  • Permissions
  • Payments
  • Errors

Let me share my experience on implementing real time updates for user object in a Rails app. There are two steps to set up a subscription for Realtime Updates:

  1. Set up a public endpoint URL that receives both HTTP GET (for subscription verification) and POST (for actual change data) requests from Facebook.
  2. Set up subscriptions for the updates we want to receive for Facebook app.

Step 1: Handle GET and POST requests from Facebook in Rails app.

  • Added a method in rails controller to handle both GET and POST requests.
    [source language=”ruby”]
    def subscription
    if request.method == “GET”
    if params[‘hub.mode’] ==’subscribe’ && params[‘hub.verify_token’] ==’stringToken’
    render :text => params[‘hub.challenge’]
    else
    render :text => ‘Failed to authorize facebook challenge request’
    end
    elsif request.method == “POST”
    updated_obj = JSON.parse(request.body.read)
    puts updated_obj
    render :text => “Thanks for the update”
    end
    end
    [/source]
    Also updated the routes.rb as
    [source language=”ruby”]
    match “facebook/subscription”, :controller => :facebook_realtime_updates, :action => :subscription, :as => ‘facebook_subscription’, :via => [:get,:post]
    [/source]

Step 2: Set subscriptions for user object in Facebook app developer dashboard.

  • We need to create an app in Facebook app developer dashboard https://developers.facebook.com/apps
  • After the app was created, go to the Settings -> Realtime Updates
    real_time_before_update
  • Select -> Add a Subscription to Get Started. This will show a dialog to choose which object we need realtime updates.
    selected_get_started
  • Select -> Confirm. Then fill the Fields, Callback and Verify token. added_for_user
    Here we added “likes,checkins,feed” fields for user object. So any of these fields get updated by user of this app will get POST call specified in the callback field. This should not be localhost/ Heroku(btw i learnt an interesting fact in Heroku that it is fully multi-threaded to support fb realtime). Verify token field is for verification that we specified in the callback server.
  • After adding the required fields, click “Test”. This will generate a GET request for the callback url when we add/modify a subscription. This GET request contains a query string with following parameters.
    • hub.mode – The string “subscribe” is passed in this parameter
    • hub.challenge – A random string
    • hub.verify_token – The verify_token value we specified when created the subscription

    Now, the rails app should then echo just the hub.challenge value back, which confirms to Facebook that this server is configured to accept callbacks, and prevents denial-of-service (DDoS) vulnerabilities.

  • If everything handled successfully, following dialog will appear with hub.challenge value.
    response_for_subscription
  • After these steps, the Facebook app is now ready to receive realtime updates. For each update of user using this app, Facebook will make POST request to the callback server within a couple of minutes. The response we get for any updates in subscribed fields will have the following format.
    [source language=”ruby”]
    {
    “object”: “user”,
    “entry”: [
    {
    “uid”: “100003239436215”,
    “id”: “100003239436215”,
    “time”: 1362240615,
    “changed_fields”: [
    “feed”,
    “likes”
    ]
    }
    ]
    }
    [/source]
    In this response, the uid and id fields are same which is the id of user. The changed_fields represents two field changes were batched together. Note that this response does not include the actual data values (either from before or after the update). To obtain those, our app can request via GET request to Graph API.

Hope you found this blog useful. If you are looking for live demo please let me know via the comments so that I can host for you.

Related posts:

Leave a Reply

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