Reimers.dk

.NET, AJAX and Google APIs brought together
Welcome to Reimers.dk Sign in | Join | Help
in Search

Jacob Reimers Weblog

Starting XMPP with .NET

If you haven't heard about XMPP before then I suggest that you find out what it is. In short, it's the protocol that Google Talk uses. But XMPP is so much more, and if you want to integrate real-time communication in your application, for example for multi-user collaboration, like Google Wave, then XMPP is a good option.

XMPP and Jabber hasn't really been adopted by the .NET community yet. The most well known XMPP project for .NET is jabber-net which is a very complete library, but I wasn't too happy with the way it uses string parsing everywhere. For that reason, and for a bit of self-learning I decided to sit down and read the specifications and get to work on implementing my own library. Mind you it is still very much in development, but I thought I would put it out there and get feedback.

The main class is the XmppClient class that will allow you to connect to an XMPP server an send and receive messages. Simply create an instance of the XmppClient class and establish a connection with your JID.

C#

XmppClient communicator = new XmppClient();
communicator.Connect("jabber.id@domain.com", "password");

The Jabber ID will most likely be your GMail account. Though if you want to start to integrate XMPP into your own applications I would recommend setting up your own server (Openfire is a good free choice).

One of the most difficult things about setting up the connection is finding out which server address to connect to. The built in DNS features in .NET don't support resolving SRV records, so I'm using the DnDNS library. It's not completely finished but it's the best option for the moment. Implementing it is in no way intended to violate any copyrights.

Establishing the communication is only half the story, but it's the story so far. Once you have the connection then you are ready to send and receive data back and forth. When an incoming stanza arrives it will trigger an event, so before calling the Connect method as shown before it is a good idea to have registered your event handlers, especially for the different stanza received events.

XMPP is about connecting to a federated network, so you may be receiving stanzas from many sources and they may be coming in fast (I don't think the Openfire server was named by accident). The XmppClient triggers the events in threads so as not to stop receiving stanzas while one is being processed, so you should take care to make your application thread safe.

The X in XMPP is for extensible (as everywhere else) and XMPP lets you transmit pretty much anything as extensions to the base stanzas (the basic XML structure for transmitting data in XMPP). The IContentStanza interface defines the core interface for an extensible stanza, and the three "base" stanzas, The PresenceStanza, MessageStanza and InfoQueryStanza implement it. This means that you can find all extended content in the ExtendedContent property. Since there is no real limit on what can be added as extended content, your application will need to pick out and parse what it needs and here LINQ for XML is your friend.

Apart from stabilizing the core communicator this is the part that is going to be under development and extension in the future. The idea is to develop built-in support for the most common extensions, but leave the extensibility open so you can fit it into your own application.

As always, the code is available for your private or commercial use. It is copyrighted, so don't remove the credits and pass it off as your own.

Published 24. oktober 2009 17:11 by jjrdk
Filed under:

Comments

 

PAD said:

I am interested in an alternative to Jabber.net for XMPP communication so your post is of interest. Perhaps I am being a little stupid but I can't locate your code to try it out - can you help please?

februar 14, 2010 13:31
 

jjrdk said:

It's not published yet, but you can get it from the SVN trunk - svn://svn.reimers.dk/reimers or you can get it from GitHub - http://github.com/jjrdk/reimers-public/tree/master/Reimers/

februar 20, 2010 09:05
Anonymous comments are disabled