Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Facebook Chat Client for Windows Phone

From Wiki
Jump to: navigation, search

This code example introduces the FacebookChat Library and shows how it can be used to create a Facebook chat client application.

WP Metro Icon Messages.png
WP Metro Icon Web.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
Devices(s): Nokia Lumia 820,920
Dependencies: Facebook API
Platform Security
Created: somnathbanik (19 Sep 2013)
Last edited: hamishwillee (07 Oct 2013)



This article introduces the FacebookChat Library, a C# library that allows you to connect to and use the Facebook Chat API (which presents an XMPP interface for third party clients).

The article provides an overview of the library API, along with information about how to authenticate the client using X-FACEBOOK-PLATFORM.

The accompanying code example creates a basic chat client using the library. Note that the UI of the application is not discussed as it’s not the main focus. You can however use the code as an example of a basic UI, or as the basis of your own beautiful UI.

Facebook Client Library API

The main methods in the client library are listed below:

  • OnLoadedFriendList : Returns Facebook friend list
  • OnLoadedMessageReceived : Returns with Chat message from user
  • OnLoadedOnlineFriends : Returns list of Online/Offline friends
  • OnLoadedUserConnected : Returns user connected status
  • OnLoadedUserConversationStateChanged : Returns user conversation state
  • OnLoadedUserDisconnected : Called when user is disconnected
  • OnLoadedUserStateChanged : Returns user session state changed
  • Connect() : Create a chat session
  • SendMessage() : Send message to user

Creating a Facebook Client

The main steps for creating a client using the Facebook client library are:

  • Create a standard Windows Phone project using Visual Studio - based on template Windows Phone App (Visual C# Template)
  • Download the Facebook Client library and add to your project reference
  • Authenticate your client application with xmpp_login extended permission.
  • Get the access_token and save it for future use.
  • Follow the #Using the Facebook Client Library section below to make use of the library.


You can authenticate the Facebook Chat client using either X-FACEBOOK-PLATFORM or DIGEST-MD5. In the code example we use the recommended X-FACEBOOK-PLATFORM mechanism to connect to the Facebook Chat server. In order to connect using this mechanism, the user must first log in to the developed application and grant the xmpp_login extended permission, then follow the application authentication process to get an access_token using the xmpp_login extended permission.

Once we have the access_token the application can log in to Facebook Chat via XMPP using the X-FACEBOOK-PLATFORM mechanism. For more details about Facebook Chat client authentication and implementation see Facebook Chat API.

We don't go into greater detail on authentication here, but you can get the complete application authentication process in the example source code or follow the below articles for better understanding:

Using the Facebook Client Library

  • Once you are done adding the library in the project reference add the using statement to call the APIs.
    using XFacebook.Chat.WindowsPhone.XFACEBOOK;
  • Create an instance of XFacebook class in App.xaml.cs file.
    public static IXFacebook iXFacebook = new XFacebook.Chat.WindowsPhone.XFACEBOOK.XFacebook();
  • Call the event OnLoadedOnlineFriends to load all online Facebook friends and display on a list box.
    App.iXFacebook.OnLoadedOnlineFriends += iXFacebook_OnLoadedOnlineFriends;
    void iXFacebook_OnLoadedOnlineFriends(object sender, FriendsStatusItemsArgs e)
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    txt_OnlineFriends.Text = "Online Friends";
    ListBoxFriends.ItemsSource = e.ListFriendsStatusItems;
  • Call Connect() method to start a session which takes Facebook Access Token and Facebook Key as parameters.
    App. iXFacebook.Connect(Global.GAccessToken, Global.FacebokKey);
  • Event OnLoadedMessageReceived is called when you receive message from user.
    App.iXFacebook.OnLoadedMessageReceived += iXFacebook_OnLoadedMessageReceived;
    void iXFacebook_OnLoadedMessageReceived(object sender, NewMessageItemsArgs e)
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    if (e.iNewMessageItems.IsReceived)
    MessagesViewModel viewModel = this.DataContext as MessagesViewModel;
    CustomMessage customMessage = new CustomMessage(e.iNewMessageItems.Message, DateTime.Now.AddMinutes(1), ConversationViewMessageType.Incoming, viewModel.ConversationBuddy.PersonId);
  • SendMessage() method is used to send chat message, which takes user id and text message as parameter.
    App.iXFacebook.SendMessage(originalMessage.Text, Global.GSelectedFriendId);
  • To get user conversation state changed, call the event OnLoadedUserConversationStateChanged
    App.iXFacebook.OnLoadedUserConversationStateChanged += iXFacebook_OnLoadedUserConversationStateChanged; 
    void iXFacebook_OnLoadedUserConversationStateChanged(object sender, ConversationStateItemsArgs e)
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    txt_ConversationState.Text = e.iConversationStateItems.State;
  • Before you run the application make sure to add your Facebook Key and Secret in Global.cs file in project folder.
  •   public const string FacebokKey = "<YourKey>";
    public const string FacebokSecret = "<YourSecret>";


During the course of the development of this library I went through many XMPP libraries for .Net and C++. Though I liked some, most of them were paid or did not support X-FACEBOOK Chat implementation. The most helpful for me was XMPP/Media library for .Net and Windows Phone 7.5 which saved much of my time implementing a complete XMPP stack.

Source Code

Library Release History

Version 1.1

In this release we have taken care of

  • Displaying both Online/Offline friend list
  • Added INotifyPropertyChanged interface to update the UI

Version 1.0

Known Issue

Note.pngNote: This issue has been addressed in the latest release of the library File:FacebookChat Library

Sometimes you might come across to a situation where you don't see friend list on the screen. This is not because of any issue in the library but the application list box which doesn't update when item gets added in the list. For verification call the event OnLoadedOnlineFriends and see what you get in the response.

 void iXFacebook_OnLoadedOnlineFriends(object sender, FriendsStatusItemsArgs e)
Deployment.Current.Dispatcher.BeginInvoke(() =>
txt_OnlineFriends.Text = "Online Friends";
ListBoxFriends.ItemsSource = e.ListFriendsStatusItems;

Use ObservableCollection or some other way to get better notified when there are changes in the list items.


I started working on this library when I saw the DiBo post. And took it as a challenge after it got featured.

There are many areas for improvement in the library, so if you have any suggestions or find bugs while using this library please keep coming your feedback in the comment section to help me update the library. We will come up with source code of the Library in our next release.

This page was last modified on 7 October 2013, at 10:48.
236 page views in the last 30 days.