Sunday, July 03, 2011

Building XMPP Client using MatriX XMPP SDK


I decided to create my own XMPP client application which would allow me to have a full two-way communication. To achieve this, I used a Matrix XMPP SDK / Library written in C# and dedicated to .NET and Silverlight technologies. It will be written as a console application, however it should not be a problem to transfer it to a small Windows application in the latter period.

XMPP is a shortcut of Extensible Messaging and Presence Protocol, which is an open standard communication protocol for message-oriented middleware based XML. This protocol was originally named Jabber as it was developed by Jabber open-source community in 1999, for instance messaging. Advantages of the XMPP is that it is decentralized, and its architecture is similar to email, meaning that anyone can run its own XMPP server and there is no central master server.
It is secure as SASL and TLS have been built into the core XMPP specification. However, XMPP has its disadvantages as well. One of them is that in-band binary data transfer is very inefficient. Because XMPP is not encoded as efficient XML interchange but as single long XML document, binary data must first be base64 encoded before it can be transmitted in-band. The most important Jabber terms used in this article are:

    JID: Jabber ID, looks like an email address (e.g. where is the Jabber server (you can find a list of public Jabber servers here).
    Roster: List of contacts (friends, family). The Roster can be organized like a tree (as the Jabber protocol is XML based) with folders and the JIDs.


As an open and standardized protocol, there are plenty of libraries for different programming languages and operating systems available. As I was looking for a C# library, I found the Matrix XMPP library dealing with the most important tasks like Jabber Client or even Jabber Server development. The Matrix XMPP is provided under two licenses, the GPL for open source project, and a commercial license for closed source projects.

To get started with the Matrix XMPP library, you can download the SDK here. Matrix is available for the full .NET Framework, .NET Compact Framework and Silverlight. This allows you to reuse all your existing code on all major .NET platforms.
My Implementation

This application is just a demonstration of how to create a Jabber Client. It does not store any connection data or provide creation of a new account. So if you would like to use the code to develop a real world client, you will have to add some more functionality to the code. The best way to do that is to follow and read the Matrix XMPP developer tutorial.

For this example, I have chosen a console application because it makes the code more readable. As the Matrix XMPP library provides a lot of events to handle several functionalities that do not make a console application the first choice, I think a Windows Forms Application would make it all a little bit confusing on the first view. So the goals of this example application are to:

    Connect to a Jabber server and authenticate the Jabber user
    Send the users presence over to the server
    Handle the presence of the contacts in our Roster (receive all available contacts)
    Chatting functionality (sending and receiving chat messages)

To connect to a Jabber server, we need a JID (Jabber ID) and the corresponding password. At first, we have to create a new JID object with the user's JID as a string parameter.
After that, we can create a new XmppClient object with the JID server property of the JID object as a constructor parameter. Then, we have to open the connection and wait until we are connected and authenticated. The code looks like this:

Console.Title = "Faruk Pasic - XMPP App using Matrix XMPP SDK!";
Console.WriteLine("\t**** Welcome to Faruk Pasic XMPP Messenger Interface ****");
Console.WriteLine("\nJabber ID: ");
string JID_sender = Console.ReadLine();
Console.WriteLine("Password: ");
string Password = Console.ReadLine();
// Connect to Jabber Account!
Jid jidsender = new Jid(JID_sender);
XmppClient xmpp = new XmppClient();
xmpp.Password = Password;
xmpp.Status = "I am Online!";
xmpp.Show =;
// Trying to connect...if not send error message!
try { xmpp.Open(); xmpp.OnLogin += new EventHandler<matrix.eventargs />(xmpp_OnLogin); }
catch { Console.WriteLine("Wrong login data!"); }

Read more: Codeproejct
QR: BuildingXMPPClient.aspx

Posted via email from Jasper-Net