Click here to Skip to main content
Click here to Skip to main content
Go to top

Building XMPP Client using MatriX XMPP SDK

, 1 Jul 2011
Rate this:
Please Sign up or sign in to vote.
How to build XMPP Client using MatriX XMPP SDK

Introduction

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

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. Test@TestServer.com) where TestServer.com 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.

MatriX XMPP SDK

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.SetUsername(jidsender.User); 
xmpp.SetXmppDomain(jidsender.Server); 
xmpp.Password = Password; 
xmpp.Status = "I am Online!"; 
xmpp.Show = Matrix.Xmpp.Show.chat; 
// 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!"); }

In order to receive the presence of our Roster members, we have to register the OnPresence Eventhandler. The code is:

Console.WriteLine("Available Contacts: "); 
xmpp.OnPresence += new EventHandler<presenceeventargs />(xmpp_OnPresence); 
xmpp.AutoRoster = true;

Now comes the most interesting part which is chatting. It consists of two parts: sending and receiving messages. The sending part is executed by xmpp.Send() method while receiving uses xmpp.OnMessage EventHandler on method xmpp_OnMessage. This process of sending and receiving messages is enclosed in the while loop and will terminate after we enter the statement: Q!. After that, we just have to close the XMPPClient by calling xmpp.Close() and we have terminated MyXMPP application. All this is presented by the following code statements:

Console.WriteLine("\n*** Start Chat ***"); 
xmpp.OnMessage += new EventHandler<messageeventargs />(xmpp_OnMessage); 
string outMessage; 
bool halt = false; 
do { Console.ForegroundColor = ConsoleColor.Green; 
outMessage = Console.ReadLine(); 
if (outMessage == "q!") 
{ halt = true; } 
else { xmpp.Send(new Message(new Jid(jid), MessageType.chat, outMessage)); } } 
while (!halt); 
Console.ForegroundColor = ConsoleColor.White; 
Console.ReadLine(); 
xmpp.Close();

Updates

As the MatriX SDK is a rather new kit, I will strive to update this basic client with more processes as time goes.

For all updates, questions and comments, check my personal webpage at http://www.faruk.ba/.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Faruk Pasic
Software Developer Unis Energetika
Bosnia And Herzegovina Bosnia And Herzegovina
More on www.faruk.ba

Comments and Discussions

 
QuestionHow To send XML Request to XMPP server (Without matrix) Pinmemberivasu5-Sep-14 19:15 
QuestionBuilding XMPP Client using MatriX XMPP SDK in WP7 PinmemberDeepakviiv1-Feb-13 0:00 
QuestionLicense Pinmemberalexgnauck25-Oct-12 22:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 1 Jul 2011
Article Copyright 2011 by Faruk Pasic
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid