Implementing Extensible Messaging And Presence Protocol (XMPP) In Windows Phone

XMPP is a protocol used for real time Extensible Messaging and Presence Protocol, a set of instant messaging, presence, multi-party chat, voice and video calls, collaboration, lightweight middleware, content syndication, and generalized routing of XML data.

XMPP

For more information on XMPP please visit xmpp.org.

Facebook WhatsApp is an example of chat application using XMPP protocols.

It is really tough to implement XMPP in Windows Phone, because there is no free client library. There is some paid libraries which is highly expensive.

After a few days of research I found one library from xmedianet which works on windows phone 8 but there is no proper documentation.

Here I am going to explain the implementation.

Get the library from the following: XMPP/Media Library for .NET and Windows Phone 7.5.

Now create new Windows Phone Project and name it as your wish.

Add the phone.socketserver.dll and phone.xmpp.dll in your project which is already downloaded from xmedianet like the following screen.

downloaded

liberary

Now design the screen like below to perform the xmpp implementation.

Login

Next in your code page (MainPage.cs) add the following code:

Firstly, refer the library namespace as we added earlier.

  1. using xmedianet.socketserver;  
  2. using System.Net.XMPP;  
Declare the following variables for xmpp chat implementation:
  1. public String UserName  
  2. {  
  3.     get;  
  4.     set;  
  5. }  
  6. public String PassWord  
  7. {  
  8.     get;  
  9.     set;  
  10. }  
  11. private Boolean IsXmppSuccess  
  12. {  
  13.     get;  
  14.     set;  
  15. }  
  16. private String Server = "www.google.in"// replace your server details  
  17. private String ServerIPAddress = "123.4.5.6:8080"// replace your ip  
  18. public XMPPClient ObjXmppClient  
  19. {  
  20.     get;  
  21.     set;  
  22. }  
  23. public XMPPConnection ObjXmppCon  
  24. {  
  25.     get;  
  26.     set;  
  27. }  
  28.   
  29. In your login button click event write the below code  
  30. for connecting to XMPP server  
  31. private void loginBtn_Click(object sender, RoutedEventArgs e)  
  32. {  
  33.     UserName = useridtxtbox.Text.ToString();  
  34.     PassWord = passwordtxtbox.Text.ToString();  
  35.     ObjXmppClient = new XMPPClient();  
  36.     ObjXmppClient.JID = UserName + "@" + Server;  
  37.     ObjXmppClient.Password = PassWord;  
  38.     ObjXmppClient.Server = ServerIPAddress; //user server for emulator and ip address for device.  
  39.     ObjXmppClient.AutoReconnect = true;  
  40.     ObjXmppClient.RetrieveRoster = true;  
  41.     ObjXmppClient.PresenceStatus = new PresenceStatus()  
  42.     {  
  43.         PresenceType = PresenceType.available, IsOnline = true  
  44.     };  
  45.     ObjXmppClient.AutoAcceptPresenceSubscribe = true;  
  46.     ObjXmppClient.AttemptReconnectOnBadPing = true;  
  47.     ObjXmppCon = new XMPPConnection(ObjXmppClient);  
  48.     ObjXmppCon.Connect();  
  49.     ObjXmppClient.Connect();  
  50.   
  51.     //Initialize event handler  
  52.     ObjXmppCon.OnAsyncConnectFinished += ObjXmppCon_OnAsyncConnectFinished;  
  53.     ObjXmppClient.OnStateChanged += new EventHandler(xMPPClient_OnStateChanged);  
  54.   
  55.     ObjXmppClient.OnXMLReceived += ObjXmppClient_OnXMLReceived; //for all xml  
  56.   
  57.     ObjXmppClient.OnNewConversationItem += ObjXmppClient_OnNewConversationItem; //for receive message  
  58.     ObjXmppClient.OnRetrievedRoster += ObjXmppClient_OnRetrievedRoster;  
  59. }  
  60.   
  61. OnAsyncConnectFinished is the event handler  
  62. for xmpp connection.After finishing the connection this event will be fired and I set the IsXmppSuccess value like below.  
  63. void ObjXmppCon_OnAsyncConnectFinished(SocketClient client, bool bSuccess, string strErrors)  
  64. {  
  65.     IsXmppSuccess = client.Connected;  
  66. }  
  67. OnStateChanged is the event handler  
  68. for xmpp connection states status  
  69. while connecting to xmpp server there is different connection states stated below as enum  
  70. public enum XMPPState  
  71. {  
  72.     Unknown = 0,  
  73.         Connecting = 1,  
  74.         Connected = 2,  
  75.         Authenticating = 3,  
  76.         Authenticated = 4,  
  77.         AuthenticationFailed = 5,  
  78.         CanBind = 6,  
  79.         Binding = 7,  
  80.         Bound = 8,  
  81.         Sessioning = 9,  
  82.         Session = 10,  
  83.         Ready = 11,  
  84. }  
  85. If XMPPState is ready means your credentials are correct.  
  86. private void xMPPClient_OnStateChanged(object sender, EventArgs e)  
  87. {  
  88.     try  
  89.     {  
  90.         switch (ObjXmppClient.XMPPState)  
  91.         {  
  92.         case XMPPState.Ready:  
  93.             if (IsXmppSuccess)  
  94.             {  
  95.                 //sucessfully logged in  
  96.             }  
  97.             break;  
  98.         }  
  99.     }  
  100.     catch (Exception ex)  
  101.     {  
  102.   
  103.     }  
  104. }  
To send message write the following code in your send button click event, if you are going to send the message first time then add the roster(to Jid) in your contact to get the presence of the rosters.

To add roster in your contact:
  1. ObjXmppClient.AddToRoster("[email protected]""User Name""Group name");  
  2.   
  3. private void sendChatBtn_Click(object sender, RoutedEventArgs e)  
  4. {  
  5.     JID tojid = new JID("toaddress");  
  6.     ObjXmppClient.SendChatMessage("Hi New text message", tojid);  
  7. }  
OnNewConversationItem is the event to receive the message from other clients. In this event you will get the new message and send messages.
  1. void ObjXmppClient_OnNewConversationItem(RosterItem item, bool bReceived, TextMessage msg)  
  2. {  
  3.     if (bReceived)  
  4.         ShowMessage("Received: " + msg.Message.ToString());  
  5.   
  6.     else  
  7.         ShowMessage("Send: " + msg.Message.ToString());  
  8.   
  9. }  
OnXMLReceived is the event to receive all the xml because all your xmpp communication is processed in XML format.
  1. void ObjXmppClient_OnXMLReceived(XMPPClient client, string strXML)  
  2. {  
  3.    //all xml received  
  4. }  
Notes

 

  • JID- Jabber ID a unique address for each rosters like Gmail id.
  • Roster- User contact is called rosters.
  • Presence-User offline/online/away etc.

Up Next
    Ebook Download
    View all
    Learn
    View all