Click here to Skip to main content
Click here to Skip to main content

Jabber Client using MatriX XMPP Library

, 9 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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

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)

Share

About the Author

K.Kirivarnan
Software Developer
Sri Lanka Sri Lanka
No Biography provided
Follow on   Twitter

Comments and Discussions

 
Questionhow could i recognize that it is connected to server or not PinmemberMember 112828523-Dec-14 4:08 
QuestionBut Matrix is not a free library Pinmembervaibhavtiwari26017-Sep-14 0:10 
QuestionHow To send XML Request to XMPP server (Without matrix) Pinmemberivasu5-Sep-14 20:14 
QuestionServerless... Is it possible? PinmemberAnastasios Mavrommatis11-Feb-14 4:07 
Questionnot able to connect to jabber.org Pinmemberrabti ranjan30-Jul-12 6:44 
QuestionJabber client with GTalk Pinmemberballchris1313-Sep-11 6:55 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 9 Sep 2011
Article Copyright 2011 by K.Kirivarnan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid