In this part of the tutorial, we will discuss how to implement the real-time updates feature in your application.

How to Subscribe for the Real-Time Updates

The process of registering a subscription for the real-time updates can be broken down into 2 parts:

Sending the Subscription Request to Facebook

To subscribe for the real-time update, an HTTP POST request should be sent to:

Sample code (with the PHP SDK used) are given below:

 $param = array('access_token' => $access_token,
                'object' => 'user',
                'fields' => 'name,feed,email',
                'callback_url' => $appCallbackUrl.'rtupd_callback.php',
                'verify_token' => $rtupd_verify_token
                );
 $subs = $facebook->api('/'.$appid.'/subscriptions', 'POST', $param); 

Completing the subscription verification process

Right after you send the subscription request out, Facebook will verify the subscription by sending HTTP GET request to the callback script in the subscription request..

A sample dump of the $_GET parameter of this HTTP GET request (by using PHP code) is shown below:

 Array
 (
    [hub_mode] => subscribe
    [hub_challenge] => 1331028284
    [hub_verify_token] => Our1stFBApp
 )
 
What you need to do in this "challenge-and-response" verification process is to simply return back to Facebook the value of the parameter "hub_chanllenge".

Sample code for handling this subscription verification request is shown below:

$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' && $_GET['hub_verify_token'] == 'Our1stFBApp') {
   echo $_GET['hub_challenge'];
   exit;
}

 

Enquiring and Deleting the Current Subscription

 Array
 (
   [data] => Array
      (
        [0] => Array
          ( [object] => user
            [callback_url] => http://www.takwing.idv.hk/facebook/demoapp_phpsdk/rtupd_callback.php
            [fields] => Array
              (
                 [0] => feed
                 [1] => name
              )
            [active] => 1
          )
      )
 ) 
$param = array('access_token' => $access_token);
$subs = $facebook->api('/'.$appid.'/subscriptions', 'DELETE', $param);

 

In the next part of the tutorial, we will discuss about the change notification.