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:

Reading Out the Contact List
Sending Messages
Receiving Messages
Complete Chat Client
Determine Status "Typing"
Advanced Chat Client

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:

using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.protocol.iq.roster;
using agsXMPP.Collections;

Then we can prepare the connection to the Facebook server (chat.facebook.com) with:

XmppClientConnection xmpp = new XmppClientConnection("chat.facebook.com");

Before the actual logging in we store a function as reaction to the OnLogin event. This is, because the logging in occurs asynchronous, as soon as it is done the event occurs:

xmpp.OnLogin += new ObjectHandler(OnLogin);

The function looks as follows, basically all commands it are optional, the last 3 lines of codes change our login status to online:

private void OnLogin(object sender)
{
    MessageBox.Show("Login successful.");
    Presence p = new Presence(ShowType.chat, "Online");
    p.Type = PresenceType.available;
    xmpp.Send(p);
}

After that we can connect with our data. Here the Facebook name (the name in the URL of the profile, e.g. for https://www.facebook.com/hans.wurst hans.wurst) and the password is needed:

xmpp.Open("name""password");

The complete code could look as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using agsXMPP;
using agsXMPP.protocol.client;
using agsXMPP.protocol.iq.roster;
using agsXMPP.Collections;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        XmppClientConnection xmpp = new XmppClientConnection("chat.facebook.com");

        private void Form1_Load(object sender, EventArgs e)
        {
            xmpp.OnLogin += new ObjectHandler(OnLogin);
            xmpp.Open("""");
        }

        private void OnLogin(object sender)
        {
            MessageBox.Show("Login successful.");
            Presence p = new Presence(ShowType.chat, "Online");
            p.Type = PresenceType.available;
            xmpp.Send(p);
        }
    }
}

13 comments:

  1. hi! Good work!
    I really appreciate the work you done!
    NO DOWNLOAD LINK..!
    Will you please mail me the Facebook Chat related Samples..!
    Email: shekharshete1987@gmail.com

    One more thing can we use same for ASP.NET Web Applications?

    Please write some blogs on FacebookChat in Asp.net and let me know..!

    Thanks,
    www.shekharshetemcts.wordpress.com

    ReplyDelete
  2. Hi, That this example looking very nice.
    Please provide me c# code for facebook chat.
    My email is sudeshchhipa102@gmail.com
    I'll use this on windows application.
    May I use this on web applications?
    Thanks

    ReplyDelete
  3. 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.

    ReplyDelete
  4. hi nice work man....
    please send me code for help...
    asgharali.ntu@gmail.com

    ReplyDelete
  5. 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

    Thanks.

    ReplyDelete
  6. hi Oliver, please send the code om my email : batrashish@gmail.com

    Regards,
    Ashish

    ReplyDelete
  7. Hi guys,

    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.rar
    Enjoy!

    ReplyDelete
  8. Thanks man. u are awesome

    ReplyDelete
  9. Kindly send me as soon as possible
    rehankhantht@gmail.com

    ReplyDelete
  10. Tutorials is not run ? Can you explain for me okay? I have provided url and pass.

    Thanks you very much.

    ReplyDelete
  11. Hey guys,

    as mentioned in the post unfortunately this does no longer work.

    Facebook disabled this function :-(

    ReplyDelete
  12. hi guy
    Please sent your code on my email.Thank you very much.
    email weerazero@gmail.com

    ReplyDelete