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. In your login button click event write the below code
  29. for connecting to XMPP server
  30. private void loginBtn_Click(object sender, RoutedEventArgs e)
  31. {
  32. UserName = useridtxtbox.Text.ToString();
  33. PassWord = passwordtxtbox.Text.ToString();
  34. ObjXmppClient = new XMPPClient();
  35. ObjXmppClient.JID = UserName + "@" + Server;
  36. ObjXmppClient.Password = PassWord;
  37. ObjXmppClient.Server = ServerIPAddress; //user server for emulator and ip address for device.
  38. ObjXmppClient.AutoReconnect = true;
  39. ObjXmppClient.RetrieveRoster = true;
  40. ObjXmppClient.PresenceStatus = new PresenceStatus()
  41. {
  42. PresenceType = PresenceType.available, IsOnline = true
  43. };
  44. ObjXmppClient.AutoAcceptPresenceSubscribe = true;
  45. ObjXmppClient.AttemptReconnectOnBadPing = true;
  46. ObjXmppCon = new XMPPConnection(ObjXmppClient);
  47. ObjXmppCon.Connect();
  48. ObjXmppClient.Connect();
  49. //Initialize event handler
  50. ObjXmppCon.OnAsyncConnectFinished += ObjXmppCon_OnAsyncConnectFinished;
  51. ObjXmppClient.OnStateChanged += new EventHandler(xMPPClient_OnStateChanged);
  52. ObjXmppClient.OnXMLReceived += ObjXmppClient_OnXMLReceived; //for all xml
  53. ObjXmppClient.OnNewConversationItem += ObjXmppClient_OnNewConversationItem; //for receive message
  54. ObjXmppClient.OnRetrievedRoster += ObjXmppClient_OnRetrievedRoster;
  55. }
  56. OnAsyncConnectFinished is the event handler
  57. for xmpp connection.After finishing the connection this event will be fired and I set the IsXmppSuccess value like below.
  58. void ObjXmppCon_OnAsyncConnectFinished(SocketClient client, bool bSuccess, string strErrors)
  59. {
  60. IsXmppSuccess = client.Connected;
  61. }
  62. OnStateChanged is the event handler
  63. for xmpp connection states status
  64. while connecting to xmpp server there is different connection states stated below as enum
  65. public enum XMPPState
  66. {
  67. Unknown = 0,
  68. Connecting = 1,
  69. Connected = 2,
  70. Authenticating = 3,
  71. Authenticated = 4,
  72. AuthenticationFailed = 5,
  73. CanBind = 6,
  74. Binding = 7,
  75. Bound = 8,
  76. Sessioning = 9,
  77. Session = 10,
  78. Ready = 11,
  79. }
  80. If XMPPState is ready means your credentials are correct.
  81. private void xMPPClient_OnStateChanged(object sender, EventArgs e)
  82. {
  83. try
  84. {
  85. switch (ObjXmppClient.XMPPState)
  86. {
  87. case XMPPState.Ready:
  88. if (IsXmppSuccess)
  89. {
  90. //sucessfully logged in
  91. }
  92. break;
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. }
  98. }
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. private void sendChatBtn_Click(object sender, RoutedEventArgs e)
  3. {
  4. JID tojid = new JID("toaddress");
  5. ObjXmppClient.SendChatMessage("Hi New text message", tojid);
  6. }
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. else
  6. ShowMessage("Send: " + msg.Message.ToString());
  7. }
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