Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#
Article

Jabber Client using MatriX XMPP Library

Rate me:
Please Sign up or sign in to vote.
4.33/5 (6 votes)
9 Sep 2011CPOL2 min read 55.2K   4K   18   9
Jabber Client using Matrix XMPP Library
Jabber Client

Introduction

In this article, I'm going to explain how to implement the XMLPP client using MatriX library. My client application would allow only point to point communication.

What is MatriX Library

MatriX is a library for the eXtensible Messaging and Presence Protocol (XMPP) for the Microsoft .NET platform. MatriX can be used to build high quality and high performance XMPP software products.

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

What is XMPP

XMPP is a shortcut of Extensible Messaging and Presence Protocol, which is an open standard communication protocol for message-oriented middleware based XML. The XMPP is an open XML technology for real-time communication, which powers a wide range of applications including instant messaging, presence, media negotiation, whiteboarding, collaboration, lightweight middleware, content syndication, and generalized XML routing.

Every user on the XMLPP network has a unique Jabber ID (usually abbreviated as JID). The JID is structured like an e-mail address with a username and a domain name for the server where that user resides, separated by an at sign (@), such as kirivarnan@jabber.org.

To get a better idea about Xmpp, check out Wiki XMPP.

Implementation

To use the MatriX, you need to add Matrix.dll as a reference. For this application, we need to include the following namespaces:

C#
using Matrix;
using Matrix.Xmpp; 
using Matrix.Xmpp.Client; 

Create a new XMLPP client instance:

C#
XmppClient xmppClient = new XmppClient();

Set the XMPP Client Properties

C#
xmppClient.Compression = false;
xmppClient.Hostname = null;
xmppClient.ResolveSrvRecords = true;
xmppClient.StartTls = true;
xmppClient.Status = "Online";
xmppClient.Show = Show.NONE;
	
Console.WriteLine("Enter your JID/UserId: (Ex: kirivarnan@jabber.org)");
string jidId = Console.ReadLine();
	
Console.WriteLine("Enter your password: ");
string password = Console.ReadLine();

Jid jid = new Jid(jidId);
xmppClient.Password = password;
xmppClient.Username = jid.User;
xmppClient.SetXmppDomain(jid.Server);

Event Handlers

C#
xmppClient.OnLogin += new EventHandler<matrix.eventargs>(XmppClient_OnLogin);
xmppClient.OnAuthError += 
	new EventHandler<matrix.xmpp.sasl.sasleventargs>(XmppClient_OnAuthError);
xmppClient.OnError += new EventHandler<matrix.exceptioneventargs>(XmppClient_OnError);
	
xmppClient.OnRosterEnd += new EventHandler<matrix.eventargs>(XmppClient_OnRosterEnd);

Open the connection and send the presence:

C#
xmppClient.Open();
xmppClient.SendPresence(Show.chat, "Online");

Get Online Users

C#
xmppClient.OnPresence += new EventHandler<presenceeventargs>(XmppClient_OnPresence);
	
private static void XmppClient_OnPresence(object sender, PresenceEventArgs pres)
{
	if(!pres.Presence.Type.Equals("unavailable"))
        Console.WriteLine("{0}@{1}  {2}", pres.Presence.From.User, 
			pres.Presence.From.Server, pres.Presence.Type);
}

Receive the Incoming Messages

C#
xmppClient.MessageFilter.Add(new Jid(JID_Receiver), 
	new BareJidComparer(), 
	new EventHandler<messageeventargs>(XmppClient_OnMessage), null);

private static void XmppClient_OnMessage(object sender, MessageEventArgs e)
{
    if (e.Message.Body != null)
    {
        Console.WriteLine("{0}: {1}", e.Message.From.User, e.Message.Body);
    }
}

Send Messages to your Chat Partner

C#
Console.WriteLine("Enter Chat Roster JID:");
string JID_Receiver = Console.ReadLine();
xmppClient.Send(new Message(new Jid(JID_Receiver), MessageType.chat, sendMessage));

Finally, close the connection

C#
xmppClient.Close();

What is BOSH

BOSH is standard for Bidirectional-streams Over Synchronous HTTP and it can be used to transport XMPP stanzas. The result is an HTTP binding (BOSH) for XMPP communications that is useful in situations where a device or client is unable to maintain a long-lived TCP connection to an XMPP server.

How to Use BOSH

If you are going to use bosh instead of socket, you need to set the transport type as BOSH.

C#
xmppClient.Transport = Matrix.Net.Transport.BOSH;
xmppClient.Uri = new System.Uri("http://localhost:5280/http-bind");

Screenshots

Jabber Client

Jabber Client

Get the Latest Binary

Get the latest version of MatriX library from here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDon't working Pin
AlexanderSemik10-Apr-19 2:08
AlexanderSemik10-Apr-19 2:08 
QuestionHow can i create JID and configure Server and client for chat Pin
Kuldeep Sahu22-Oct-15 4:44
professionalKuldeep Sahu22-Oct-15 4:44 
GeneralMy vote of 1 Pin
Mikloo19-Feb-15 12:36
Mikloo19-Feb-15 12:36 
Questionhow could i recognize that it is connected to server or not Pin
Member 112828523-Dec-14 3:08
Member 112828523-Dec-14 3:08 
QuestionBut Matrix is not a free library Pin
vaibhavtiwari26016-Sep-14 23:10
vaibhavtiwari26016-Sep-14 23:10 
QuestionHow To send XML Request to XMPP server (Without matrix) Pin
ivasu5-Sep-14 19:14
ivasu5-Sep-14 19:14 
QuestionServerless... Is it possible? Pin
Anastasios Mavrommatis11-Feb-14 3:07
Anastasios Mavrommatis11-Feb-14 3:07 
I am trying to communicate an android device with a windows phone device, through wifi.
There is no internet connection and no desktop pc available. Is it possible for these devices to "talk"?
Thanks
Questionnot able to connect to jabber.org Pin
rabti ranjan30-Jul-12 5:44
rabti ranjan30-Jul-12 5:44 
QuestionJabber client with GTalk Pin
ballchris1313-Sep-11 5:55
ballchris1313-Sep-11 5:55 

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

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