XMPP Chat Messaging in Universal (Windows Store 8.1/Phone 8.1) app

Here's how implement the basic chat mechanism using XMPP in Universal app (Windows Store 8.1/ Phone 8.1).

As we don’t have any free library of XMPP for WINRT is available, I have taken the basic reference from below link and enhancing the library to support chat features.

The referenced library shared in above link supports only single chat, I am enhanced this library to support Group user chat as well. Download the library, compile and add it to your universal app project as a reference.

Connect to XMPP server:  

Initialize XMPP Client and define the settings as below.
   private XMPP.Client xmppClient;
  this.xmppClient = new XMPP.Client();

  public void ConnectToXMPPServer()
            XmppClient.Settings.Account = “<User Name” + "@" +”<server name>”;
            XmppClient.Settings.Id = “<User Name” + "@" +”<server name>”;
            XmppClient.Settings.Password = “password”;
            XmppClient.Settings.Hostname = "<server name";

            XmppClient.Settings.Port = 5222;
            XmppClient.Settings.SSL = false;
            XmppClient.Settings.OldSSL = false;
            XmppClient.Settings.AuthenticationTypes = MechanismType.None;
            XmppClient.Settings.AuthenticationTypes |= MechanismType.DigestMD5;
            XmppClient.Settings.AuthenticationTypes |= MechanismType.SCRAM;
            XmppClient.Settings.UseKeepAlive = true;
            XmppClient.OnConnected += OnClientConnected;
            xmppClient.OnReceive += OnClientReceive;
            XmppClient.OnError += xmppClient_OnError;
            XmppClient.OnResourceBound += XmppClient_OnResourceBound;

From above code, set the account, id, Password and Host Name. Account and Id parameters are same and the format as follows.
For example, if user name is TestUser and ServerName is then account and Id format would be TestUser@
HostName is a server name, Define the Port and AuthenticationTypes as specified in code.

If you want to keep XMPPClient connection open alive, set UseKeepAlive=true.

Make sure you define the events before Connect (). During the Connect () call, XMPP client will be authenticated with server and the respective events, OnConnected, OnReceive, OnError and OnResourceBound will get fired.
OnReceive is the event at client, will be triggered for XMPP server requests. For example, when you receive the message or users presence (who are online), OnReceive will be triggered.

Join Room:
When a Chat is initiated, a chat room will be created in server.  You need to join that chat room, if you want to exchange the messages.
   JID jid = new JID(jid);
   this.XmppClient.JoinRoom(jid, nickName);

JID format is as below
string.Concat(<<RoomName>>, "@conference.")
RoomName is a Chat Room which should be created in server while initiating a new chat at first time
So in technically, we should send the message to sever, server will push that message to all chat room users in that group.

Send Message:
          var message = new message();
            message.to = string.Concat(<<RoomName>>, "@conference.");
            message.from = <<User>>;
            message.type = XMPP.tags.jabber.client.message.typeEnum.groupchat;

            var body = new body();
            body.Value = “Hi”;
            message.Account = from;

From above, to is the room for which you should send the message,from just give your name, type is nothing but a type of chat, groupchat or chat.

Receive Message:
As I discussed above, the moment message is delivered to server, server will push that message to all the assignees in that group. Here, exactly OnReceive event will be triggered for all the assignees in that group.

void OnClientReceive(object sender, XMPP.common.TagEventArgs e)
var textMessage = tag as XMPP.tags.jabber.client.message;

Code for user’s presence.
void OnClientReceive(object sender, XMPP.common.TagEventArgs e)
var presence = tag as XMPP.tags.jabber.client.presence;}

Download the XMPP library from below link

By Siva Jagan Dhulipalla   Popularity  (2622 Views)