Graph API Version

JavaScript SDK: .Event.subscribe()

This method allows you to subscribe to a range of events, and define callback functions for when they fire.

FB.Event.subscribe(event, callback)

Parameters

NameTypeDescription

event

enum{}

The name of the event type you want to subscribe to. Click for more info.

auth.authResponseChanged

string

Fired when the authResponse object has changed, which indicates that the user's access token has changed in some way.

auth.statusChange

string

Fired when the user's Facebook Login status changes.

auth.login

string

Fired when someone logs into the app using FB.login(). It is preferable to use the auth.statusChange event for this purpose.

auth.logout

string

Fired when someone logs out of the app using FB.logout(). It is preferable to use the auth.statusChange event for this purpose.

comment.create

string

Fired when someone posts a comment using a Comments Plugin on the same page.

comment.remove

string

Fired when someone deletes a comment in a Comments Plugin on the same page.

edge.create

string

Fired when someone likes the page using a Like button.

edge.remove

string

Fired when someone unlikes a page using a Like button.

message.send

string

Fired when a send button on the same page is used to share a link in a message.

xfbml.render

string

Fired when FB.XFBML.parse() completes. This indicates that all of the social plugins on the page have been loaded.

callback

function

This is the callback function that is triggered when the event is fired. Click for info about the different arguments and objects passed to the callbacks for each event type.

You can only subscribe to global events. For example, if you have more than one Like button on your page, the same callback will be triggered no matter which is clicked. You can call add multiple callbacks to the same event, by calling .Event.subscribe() again with a different callback, all the callbacks you've added will be triggered when the event takes place.

auth.authResponseChanged

authResponse

A new authResponse object is sent.

auth.statusChange

authResponse

A new authResponse object is sent.

auth.login

authResponse

A new authResponse object is sent.

auth.logout

authResponse

A new authResponse object is sent.

comment.create

object

A single response object containing information about the event.

href

string

og:url of the Comments Plugin.

commentID

string

ID of the comment that was added.

parentCommentID

string

Optional ID of the comment that this comment replied to.

comment.remove

object

A single response object containing information about the event.

href

string

og:url of the Comments Plugin.

commentID

string

ID of the comment that was added.

parentCommentID

string

Optional ID of the comment that this comment replied to.

edge.create

(string, object)

A string containing the URL of the Like button target, and an object containing the element for the Like button that was clicked.

edge.remove

(string, object)

A string containing the URL of the Like button target, and an object containing the element for the Like button that was clicked.

message.send

string

A string containing the URL that was shared in the message.

xfbml.render

none

No arguments are sent to this event's callback.

Examples

edge.create and .remove

Here's an example of a callback that can handle either the edge.create or edge.remove event:

var page_like_or_unlike_callback = function(url, html_element) {
  console.log("page_like_or_unlike_callback");
  console.log(url);
  console.log(html_element);
}

// In your onload handler
FB.Event.subscribe('edge.create', page_like_or_unlike_callback);
FB.Event.subscribe('edge.remove', page_like_or_unlike_callback);

xfbml.render

This example will log when the page has finished rendering plugins:

var finished_rendering = function() {
  console.log("finished rendering plugins");
}

// In your onload handler
FB.Event.subscribe('xfbml.render', finished_rendering);

auth.authResponseChange and auth.statusChange

The following example shows how you can track these events with a simple Facebook Login button.

// In your HTML.  You must also initialize the Facebook JavaScript SDK
<fb:login-button autologoutlink="true"></fb:login-button>

// After your onload method has been called and initial login state has
// already been determined. (See above about not using these during a page's
// init function.)
FB.Event.subscribe('auth.authResponseChange', auth_response_change_callback);
FB.Event.subscribe('auth.statusChange', auth_status_change_callback);

// In your JavaScript
var auth_response_change_callback = function(response) {
  console.log("auth_response_change_callback");
  console.log(response);
}

var auth_status_change_callback = function(response) {
  console.log("auth_status_change_callback: " + response.status);
}

After logging in you will get the following response:

"auth_response_change_callback"          <-- auth response change
[object Object]                          <-- auth response change
"auth_status_change_callback: connected" <-- status change - connected == logged in

After logging out you will get the following response:

"auth_response_change_callback"        <-- auth response change
[object Object]                        <-- auth response change
"auth_status_change_callback: unknown" <-- status change - unknown == logged out

auth.login and .logout

In this shortened example, you can use a small HTML fragment and the FB.login() and FB.logout() methods to trigger these events.

// In your HTML:
<input type="button" value="Login" onclick="FB.login();">
<input type="button" value="Logout" onclick="FB.logout();">

// In your onload method:
FB.Event.subscribe('auth.login', login_event);
FB.Event.subscribe('auth.logout', logout_event);

// In your JavaScript code:
var login_event = function(response) {
  console.log("login_event");
  console.log(response.status);
  console.log(response);
}

var logout_event = function(response) {
  console.log("logout_event");
  console.log(response.status);
  console.log(response);
}

You will get the following console output when logging in:

"logout_event"
"unknown"
[object Object]

You will get the following console output when logging out:

"login_event"
"connected"
[object Object]

comment.create and .remove

// In your HTML, include the comments plugin
<div
  class="fb-comments"
  data-href="http://url.to.your-page/page.html"
  data-numposts="5"
  data-colorscheme="light">
</div>

// In your onload method
FB.Event.subscribe('comment.create', comment_callback);
FB.Event.subscribe('comment.remove', comment_callback);

// In your JavaScript
var comment_callback = function(response) {
  console.log("comment_callback");
  console.log(response);
}

When creating or removing a comment, you will get the following response:

"comment_callback"
[object Object]

message.send

// In your HTML
<div class="fb-send" data-href="http://url.to.your.page/page.html" data-colorscheme="dark"></div>

// In your onload method
FB.Event.subscribe('message.send', message_send_callback);

// In your JavaScript
var message_send_callback = function(url) {
  console.log("message_send_callback");
  console.log(url);
}

When run, this will result in the following output to your console:

"message_send_callback"
"http://url.to.your.page/page.html"