Facebook Developers
DocsToolsSupportNewsApps
Log In
  • Social Plugins
  • Facebook Login
  • Open Graph
  • Facebook APIs
  • Games
  • Payments
  • App Center
  • Promote Your App
  • iOS
  • Android
  • JavaScript
  • PHP
  • More SDKs
  • Core Methods
    • FB.api
    • FB.init
    • FB.ui
  • Auth Methods
    • FB.getAuthResponse
    • FB.getLoginStatus
    • FB.login
    • FB.logout
  • Event Handling
    • FB.Event.subscribe
    • FB.Event.unsubscribe
  • XFBML
    • FB.XFBML.parse
  • Canvas Methods
    • FB.Canvas.Prefetcher.addStaticResource
    • FB.Canvas.Prefetcher.setCollectionMode
    • FB.Canvas.hideFlashElement
    • FB.Canvas.scrollTo
    • FB.Canvas.setAutoGrow
    • FB.Canvas.setDoneLoading
    • FB.Canvas.setSize
    • FB.Canvas.setUrlHandler
    • FB.Canvas.showFlashElement
    • FB.Canvas.startTimer
    • FB.Canvas.stopTimer

FB.Event.subscribe

Overview

Attaches an handler to an event and invokes your callback when the event fires.

Example

For example, suppose you want to record Likes in your database whenever a button is clicked:

FB.Event.subscribe('edge.create',
    function(response) {
        alert('You liked the URL: ' + response);
    }
);

Or be notified when the session changes

FB.Event.subscribe('auth.authResponseChange', function(response) {
  alert('The status of the session is: ' + response.status);
});

In both of these cases, you would replace the alert() with your handling code.

Events

Global Events to which you can subscribe:

  • auth.login - fired when the auth status changes from unknown to connected
  • auth.authResponseChange - fired when the authResponse changes
  • auth.statusChange - fired when the status changes (see FB.getLoginStatus for additional information on what this means)

The response object passed into the callback function for these events looks like the following:

{
  status: "",         /* Current status of the session */
  authResponse: {          /* Information about the current session */
    userID: ""          /* String representing the current user's ID */
    signedRequest: "",  /* String with the current signedRequest */
    expiresIn: "",      /* UNIX time when the session expires */
    accessToken: "",    /* Access token of the user */
  }
}

See Response and Session Objects section in the FB.getLoginStatus documentation for more information.

NOTE: auth.statusChange() does not have a 'status' field.


  • auth.logout - fired when the user logs out. The response object passed into the callback function looks like:
{
  status: "",         /* Current status of the session */
}


  • auth.prompt - fired when user is prompted to log in or opt in to Platform after clicking a Like button. The response parameter to the callback function contains the URL that initiated the prompt:
"http://www.example.com/login.php"


  • xfbml.render - fired when a call to FB.XFBML.parse() completes. The response parameter to the callback function is empty for this event:
""


  • edge.create - fired when the user likes something (fb:like). The response parameter to the callback function contains the URL that was liked:
"http://www.example.com/article1.php"


  • edge.remove - fired when the user unlikes something (fb:like). The response parameter to the callback function contains the URL that was unliked:
"http://www.example.com/article2.php"


  • comment.create - fired when the user adds a comment (fb:comments). The response object passed into the callback function looks like:
{
  href: "",         /* Open Graph URL of the Comment Plugin */
  commentID: "",    /* The commentID of the new comment */
}


  • comment.remove - fired when the user removes a comment (fb:comments). The response object passed into the callback function looks like:
{
  href: "",         /* Open Graph URL of the Comment Plugin */
  commentID: "",    /* The commentID of the deleted comment */
}
  • message.send - fired when the user sends a message using the send button. The response object passed into the callback function contains the URL which was sent:
"http://www.example.com/article1.php"


Best Practices

For most cases, you will want to subscribe to auth.authResponseChange rather than auth.statusChange. The response is returned as a javascript array, not encoded as JSON.

Note that for some cases, the value of response is unkeyed, but when more than one variable is returned, it contains the appropriate keys.

You can subscribe multiple callbacks to one event using different function names.

Parameters

NameTypeDescription
nameString

Name of the event.

cbFunction

The handler function.

Updated about 4 months ago
Facebook © 2013 · English (US)
AboutAdvertisingCareersPlatform PoliciesPrivacy Policy