On this blog I regularly publish articles with tips and tricks for the programming language C# .Net.
C# is a modern, object-oriented programming language, which fully ultilizes the possibilites of the .Net framework. I also write about app programming for Android via C#, as well as PHP and Matlab. The difficulty of this blog is supposed to be variable, I hope for beginners and experts there is always something to look for.
If you have questions or suggestions, I am happy about your emails.

Friday, October 18, 2013

Connect to the Facebook Chat

Notice: Since the release of the Facebook API 2.0 in April 2014 the chat function is disabled, which means that this code unfortunately is no longer runnable.

In the previous two posts (Read Data from Facebook and Publish Facebook Posts) the topic was the connection to Facebook using the Graph API. This is a simple HTTP based API, over which via HTTP requests data of Facebook can be accessed.
Today I want to explain, how to connect to the Facebook Chat via C#, which is based upon the XMPP (earlier Jabber) protocol.
XMPP is a communication protocol basing upon XMP, which is mainly used for instant messaging.
Some known programs, like Google Talk and WhatsApp use this protocol, many others, e.g. ICQ, Skype and Facebook offer a possibility for connecting.
The XMPP protocol is easy to understand and very structured, so that we can connect without much code to the Facebook chat and send and recceive messages.
Since the chat and the "normal" messaging function are not separated as earlier anymore, we can also send and receive conventional messages with the code.
Reading out the contact list is also possible.
I will introduce the chat step by step. Today I will show the inclusion of XMPP and the logging in to the Facebook service.
In the next days the following posts will come online:

Now let us come to the first part. In theory we can write our own XMPP framework, which then transmits data via sockets etc.
I will use an external libary though, nameley agsXMPP, which saves us from a lot of work.
Please install this first and install it.
If we want to use it in the project, we have to add a reference to it. For that we click on "Add Reference - Browse" and select in the installation directory \agsxmpp\bin\(matching .Net Version, probably 3.5)\(Debug | Release)\agsXMPP.dll.
In the next posts we will need the following using directives:

Hi guys,I sent you the project via Email.I created the code using the publicily available Facebook documentation which is open for everybody to use, whether you are allowed to use it though (commercially), you have to check with Facebook.

Hi!I think there's something wrong in my code, because it cannot connect facebook (It not show messagebox "Login successful"). Do you have the method to show connection status or something like this?If not, can you please mail me this c# code facebook chat.my e-mail is kwanchanok1987@hotmail.com

due to many requests, like described in the corresponding post the code for a complete Facebook chat client is available on http://bloggeroliver.bplaced.net/AdvancedFacebookChat/AdvancedFacebookChat.rarEnjoy!