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.
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.
Now design the screen like below to perform the xmpp implementation.
Next in your code page (MainPage.cs) add the following code:
Firstly, refer the library namespace as we added earlier.
- using xmedianet.socketserver;
- using System.Net.XMPP;
Declare the following variables for xmpp chat implementation:
- public String UserName
- {
- get;
- set;
- }
- public String PassWord
- {
- get;
- set;
- }
- private Boolean IsXmppSuccess
- {
- get;
- set;
- }
- private String Server = "www.google.in";
- private String ServerIPAddress = "123.4.5.6:8080";
- public XMPPClient ObjXmppClient
- {
- get;
- set;
- }
- public XMPPConnection ObjXmppCon
- {
- get;
- set;
- }
-
- In your login button click event write the below code
- for connecting to XMPP server
- private void loginBtn_Click(object sender, RoutedEventArgs e)
- {
- UserName = useridtxtbox.Text.ToString();
- PassWord = passwordtxtbox.Text.ToString();
- ObjXmppClient = new XMPPClient();
- ObjXmppClient.JID = UserName + "@" + Server;
- ObjXmppClient.Password = PassWord;
- ObjXmppClient.Server = ServerIPAddress;
- ObjXmppClient.AutoReconnect = true;
- ObjXmppClient.RetrieveRoster = true;
- ObjXmppClient.PresenceStatus = new PresenceStatus()
- {
- PresenceType = PresenceType.available, IsOnline = true
- };
- ObjXmppClient.AutoAcceptPresenceSubscribe = true;
- ObjXmppClient.AttemptReconnectOnBadPing = true;
- ObjXmppCon = new XMPPConnection(ObjXmppClient);
- ObjXmppCon.Connect();
- ObjXmppClient.Connect();
-
-
- ObjXmppCon.OnAsyncConnectFinished += ObjXmppCon_OnAsyncConnectFinished;
- ObjXmppClient.OnStateChanged += new EventHandler(xMPPClient_OnStateChanged);
-
- ObjXmppClient.OnXMLReceived += ObjXmppClient_OnXMLReceived;
-
- ObjXmppClient.OnNewConversationItem += ObjXmppClient_OnNewConversationItem;
- ObjXmppClient.OnRetrievedRoster += ObjXmppClient_OnRetrievedRoster;
- }
-
- OnAsyncConnectFinished is the event handler
- for xmpp connection.After finishing the connection this event will be fired and I set the IsXmppSuccess value like below.
- void ObjXmppCon_OnAsyncConnectFinished(SocketClient client, bool bSuccess, string strErrors)
- {
- IsXmppSuccess = client.Connected;
- }
- OnStateChanged is the event handler
- for xmpp connection states status
- while connecting to xmpp server there is different connection states stated below as enum
- public enum XMPPState
- {
- Unknown = 0,
- Connecting = 1,
- Connected = 2,
- Authenticating = 3,
- Authenticated = 4,
- AuthenticationFailed = 5,
- CanBind = 6,
- Binding = 7,
- Bound = 8,
- Sessioning = 9,
- Session = 10,
- Ready = 11,
- }
- If XMPPState is ready means your credentials are correct.
- private void xMPPClient_OnStateChanged(object sender, EventArgs e)
- {
- try
- {
- switch (ObjXmppClient.XMPPState)
- {
- case XMPPState.Ready:
- if (IsXmppSuccess)
- {
-
- }
- break;
- }
- }
- catch (Exception ex)
- {
-
- }
- }
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:
- ObjXmppClient.AddToRoster("[email protected]", "User Name", "Group name");
-
- private void sendChatBtn_Click(object sender, RoutedEventArgs e)
- {
- JID tojid = new JID("toaddress");
- ObjXmppClient.SendChatMessage("Hi New text message", tojid);
- }
OnNewConversationItem is the event to receive the message from other clients. In this event you will get the new message and send messages.
- void ObjXmppClient_OnNewConversationItem(RosterItem item, bool bReceived, TextMessage msg)
- {
- if (bReceived)
- ShowMessage("Received: " + msg.Message.ToString());
-
- else
- ShowMessage("Send: " + msg.Message.ToString());
-
- }
OnXMLReceived is the event to receive all the xml because all your xmpp communication is processed in XML format.
- void ObjXmppClient_OnXMLReceived(XMPPClient client, string strXML)
- {
-
- }
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.