Building 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
) whereTestServer.com
is theJabber
server (you can find a list of publicJabber
servers here).Roster
: List of contacts (friends, family). TheRoster
can be organized like a tree (as theJabber
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 theJabber
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/.
Post Comment
2vq3rb Thank you for your blog article.Thanks Again. Really Cool.
W4Xxn1 shared your web site in my social networks
wKqzQw This is very interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your excellent post. Also, I ave shared your website in my social networks!
fmgS39 Very good post! We will be linking to this great content on our site. Keep up the great writing.
That is a very good tip especially to those fresh to the blogosphere. Brief but very precise information Thank you for sharing this one. A must read post!
AMrSrm Thanks for sharing this good post. Very interesting ideas! (as always, btw)
Thanks-a-mundo for the article.Really thank you!
Wow, great blog.Really looking forward to read more. Fantastic.
Its hard to find good help I am forever saying that its difficult to get good help, but here is
It as exhausting to search out educated people on this matter, but you sound like you know what you are speaking about! Thanks
Im obliged for the blog.Much thanks again.
Its hard to find good help I am regularly proclaiming that its difficult to get good help, but here is
This blog was how do you say it? Relevant!! Finally I ave found something which helped me. Cheers!
Thanks for the blog post.Much thanks again. Really Great.
Regards for helping out, superb information.
There as definately a great deal to learn about this subject. I like all of the points you have made.
Thanks for the meal!! But yeah, thanks for spending
viagra without prescription! Overnight shiping! Click here now and get discount!
you heard about this new site ? Dallas SEO
I think other web-site proprietors should take this website as an model, very clean and wonderful user genial style and design, let alone the content. You are an expert in this topic!
Thanks for the article post.Much thanks again. Awesome.
Very good post. I certainly appreciate this website. Continue the good work!
Wow, great post.Much thanks again. Great.
Very nice write-up. I definitely appreciate this website. Stick with it!
Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is magnificent, let alone the content!
The Birch of the Shadow I think there may perhaps be a few duplicates, but an exceedingly helpful listing! I have tweeted this. Numerous thanks for sharing!
in particular near my personal peers. Gratitudes a ton; coming from we all.
Thank you ever so for you blog article.Thanks Again.
Looking forward to reading more. Great blog.Thanks Again. Keep writing.
Im obliged for the post.Thanks Again. Keep writing.
Really appreciate you sharing this blog post.Much thanks again. Awesome.
That as a enormous intolerably astonishing hint which have situate up. Gratitude to the remarkably amazing publish!
Oh man! This blog is sick! How did you make it look like this !
Thank you ever so for you blog. Keep writing.
Thanks for sharing, this is a fantastic post. Great.
same topics discussed here? I ad really like to be a part of
It as hard to find well-informed people in this particular subject, but you sound like you know what you are talking about! Thanks
I think this is a real great blog post.Much thanks again. Great.
Muchos Gracias for your blog post.Really looking forward to read more. Really Great.
I truly appreciate this blog article. Cool.
You have noted very interesting details ! ps decent web site. O human race born to fly upward, wherefore at a little wind dost thou fall. by Dante Alighieri.
It'аs really a great and helpful piece of information. I'аm happy that you shared this useful info with us. Please stay us up to date like this. Thanks for sharing.
dbd6Bi Thank you for sharing this fine article. Very interesting ideas! (as always, btw)
3fe2LY Thanks for the help in this question, I too consider, that the easier, the better
xDS5qV Your opinion is valueble for me. Thanks!
In it something is. Thanks for the help in this question, the easier, the better ?
When someone writes an paragraph he/she keeps the idea
Spot on with this write-up, I really suppose this web site wants way more consideration. I?ll most likely be once more to learn way more, thanks for that info.
Well I sincerely enjoyed studying it. This post provided by you is very constructive for accurate planning.
Thanks again for the blog post.Really looking forward to read more. Fantastic.