Jabber Client using MatriX XMPP Library






4.33/5 (6 votes)
Jabber Client using Matrix XMPP Library

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:
using Matrix;
using Matrix.Xmpp;
using Matrix.Xmpp.Client;
Create a new XMLPP client instance:
XmppClient xmppClient = new XmppClient();
Set the XMPP Client Properties
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
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:
xmppClient.Open();
xmppClient.SendPresence(Show.chat, "Online");
Get Online Users
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
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
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
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.
xmppClient.Transport = Matrix.Net.Transport.BOSH;
xmppClient.Uri = new System.Uri("http://localhost:5280/http-bind");
Screenshots
Get the Latest Binary
Get the latest version of MatriX library from here.