Introduction

I wanted to step a little bit deeper into instant messaging development by using the Jabber protocol. The first place where I looked for an introduction was The Code Project, but I couldn't find any article dealing with Jabber client development. So I thought it's my part to write an article on how to create a small Jabber client, after I got familiar with this stuff.

In this article, I want to introduce Jabber development by a small example application that allows you to chat with one of your Roster contacts. The example is written as a Console Application, but it should be no problem to transfer the code into a small Windows Application.

Jabber/XMPP

Jabber is an open protocol for instant messaging (like Skype). The advantage of Jabber is that as an XML based protocol, it is platform independent and clients can be developed under several operating systems. It also provides the possibility to connect to other instant messaging services like ICQ. Jabber is based on the protocol XMPP (a Jabber derivative) which was standardized in 2004.

A simplified Jabber network looks like this:

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).

In the network above, we would have the following JID's:

Client1@Server1

Client2@Server1

Client3@Server2

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.

Where C is the Client and S is the Server. This example is taken from here.

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 agsXMPP library dealing with the most important tasks like Jabber Client or even Jabber Server development. The agsXMPP is provided under two licenses, the GPL for open source project, and a commercial license for closed source projects.

To get started with the agsXMPP library, you can download the SDK here.

For more detailed information about Jabber/XMPP, you should have a look here and here and here.

Jabber Client

This example 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.

So now we are prepared to develop our own Jabber Client. For this example, I have chosen a Console Application, because it makes the code more readable. As the agsXMPP 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 what are the goals of this example application?

Create a connection 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)

Last but not least, the chatting functionality (sending and receiving chat messages)

So let's start.

References

To use the agsXMPP in your own project, you have to add a reference to agsXMPP.dll in your project. For this example, you should also add the following using directives to the header:

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

Connection and Authentication

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 XmppClientConnection object with the JID server property of the JID object as a constructor parameter. The code looks like this:

Now we have to open the connection and wait until we are connected and authenticated. In the agsXMPP library, the XmppClientConnection.Open() method is executed asynchronously, so we have to register the OnLogin Eventhandler and wait until the OnLogin event is raised. This is a beautiful solution for a Win App, but it makes a Console App not so beautiful. The code should look like this:

We are just waiting until the OnLogin event is raised (which means we are logged in and authenticated) and then exit the loop to continue our program. Now we can get some information about the Connection and Authentication state of our XmppClientConnection object:

Sending and Receiving Messages

Now we can start with the most interesting part: Chatting! How does chatting work basically? We send messages to an available chat partner (we need his JID), and we handle received messages in a MessageCallBack method. To prepare for incoming messages, we have to execute the following statement:

where the JID_Receiver is the JID object of our chat buddy. This code should be packed into a loop, to send more than one message (in the download example I have put it into a do loop, with the string q! as the exit criteria). When you are finished, you just need to close the XmppClientConnection object:

xmpp.Close();

Chat

That is all there is to creating a little Jabber Chat Client which looks like this:

The corresponding screenshots of my chat partner using PSI:

Summary

There are plenty more features of Jabber/XMPP provided in the agsXMPP library, like creating your own Jabber server or creating your own packages. I think for an entry into Jabber development under C#, it is best to start with client development. The agsXMPP SDK provides a few very detailed examples like a Windows based mini client and a server component. If you would like to dive deeper into this technology, I suggest you study these examples before you dive into the libraries' source code.

I have a question on reading incoming messages. The messages are read using onMessage() using the logged in user.

Is there any way I log in as admin user and read all the incoming messages. This option I required to read the presence of the Recipient User and send the message if the user Presence is available and send Push Notification if the Recipient User and send the message if the user Presence is unavailable.

e.g. if "User1" is sending message to "User2" by logging in as "admin" user read the incoming message and check the presence and send message or send Push Notification based on the presence.

One area I am having problem with is notifications. I wanted to transfer a notification to a device that could register in a different room. The hardware portion is resolved but getting the notification say a chat invite or a conference invite through jabber or agsXMPP is not so clear. What I am looking to do is create an event flag. If an invite or call event occurs the Boolean flag becomes true. I Then do something with that. How do you acquire the event from an invite or call?

This is targeting gmail and google talk with the phone ring service (where you "Call" a recipient)they have on the hangouts. If I can extract that information that would be great!

I originally saw the post then looked at the source code and it answers and solves a lot of problems. So..

hi
i had the same problem using Gtalk's JID, so i've register jabber80.com JID and now i have no troubles.
i think the problem is in the difference between GTalk's server name and host name (according to Miranda IM settings window )
good luck to you, sonu tak, and best regards to Frank.

ps another problem is with multiconnecting. i cannot connect to the jabber80.com server while my account have been already connected (or maybe i do something wrong). waiting for any Frank's comments

Recently I need to develop a client base on XMPP , but I don't know how to deal with the subscription , I have learned the Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence , but there are some puzzles , so I hope know how you deal with the subscription . Waitting for your answers , Thanks .

you may read the example code after you install the SDK.
After binding your handler method with the OnPresence event of the XMPPClientConnection object, you deal with the subscribing request information when you receive a presence which type is PresenceType.subscribe in your handler method.
you can accept its request using code bellow:
PresenceManager pm = new PresenceManager(_connection);
pm.ApproveSubscriptionRequest(_from);

alse you can refused using code bellow:
PresenceManager pm = new PresenceManager(_connection);
pm.RefuseSubscriptionRequest(_from);