Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Tip/Trick

Using XMPP and Openfire Server in Windows Phone 8

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
18 Feb 2015CPOL2 min read 22.8K   550   5   6
Windows Phone 8 chat application using Openfire server

Introduction

Implementing Extensible Messaging and Presence Protocol (XMPP) in Windows phone 8 is really a tough experience. Because, there is no third party library which is for free (there are some libraries which are highly expensive).

Background

Project was required with WP8 chat application using Openfire server. After many days of exploration, we found a library which works well in WP8 (** WP8- Windows phone 8) but it lacks documentation and support. I started trial and error with this library. Please find the library link below:

Pre-Requisites

  • VS 2012 Express for Mobile Edition/ VS2012 Windows Phone SDK installed or higher
  • Basic Knowledge of XAML, C#
  • Openfire server installed

Using the Code

Open VS2012 -> New project -> Select Windows Phone Template -> Select Default application -> Name it as required (here named it as WP8Xmpp) -> Select target OS version – 8.0

Step 1

Image 2

Use the DLL which is already built (you can find those in the attached sample application).

(Or use the library link mentioned above, download the highlighted files and build in separate application and get the DLL).

Step 3

Refer to those libraries, phone.socketserver.dll and phone.xmpp.dll.

Step 4

Login panel UI is shown below:

Step 5

In MainPage.xaml.cs, create some properties as shown below:

C#
/// <summary>
        /// Username of the client
        /// </summary>
        public String UserName { get; set; }
        /// <summary>
        /// Password
        /// </summary>
        public String PassWord { get; set; }

        private Boolean IsXmppSuccess { get; set; }

        private readonly String Server = "gmail.com";//ex: canarys

        private readonly String ServerIPAddress = "127.0.0.1:9090/";//

Look at the sample attached.

(Note: While passing server name, it's very difficult to judge instead of IPaddress.

But I learnt using trial and error method that is for emulator user server name and for device use server IP Address).

There are two main events during connection, ObjXmppCon_OnAsyncConnectFinished and xMPPClient_OnStateChanged.

First asyncConnectFinished should be executed, there I am setting a flag IsXmppSuccess.

Soon after, followed by statechanged event, there is a flag like below mentioned.

C#
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 , then => server and credentials are correct.

If XMPPState is AuthenticationFailed => credentials are wrong.

I am just navigating to other form Output.xaml if the XMPPState is Ready.

Step 8

If there are any issues, I will try to solve them in the above mentioned sample.

Points of Interest

This is my first try of using XMPP in Windows Phone 8. I hope it will help others.

Update

Please find the next part of the article Send and Receive Message in XMPP here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Dhruv Compusoft Consultancy Pvt Ltd
India India
Senior MES Developer in TruGlobal Pvt Ltd, Bangalore, India.

My Blog
dotnetkraft.blogspot.in
gnnrao.blogspot.in

Comments and Discussions

 
Questiondoes these work for ejabberd Pin
Member 124908781-May-16 21:54
Member 124908781-May-16 21:54 
Questionhow to use it for MUC Pin
adi_201225-Mar-15 19:35
adi_201225-Mar-15 19:35 
GeneralMy vote of 5 Pin
Pradeepkumar patil sonu22-Feb-15 19:00
Pradeepkumar patil sonu22-Feb-15 19:00 
GeneralRe: My vote of 5 Pin
Nandakishore G N22-Feb-15 19:01
professionalNandakishore G N22-Feb-15 19:01 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun18-Feb-15 19:53
Humayun Kabir Mamun18-Feb-15 19:53 
GeneralRe: My vote of 5 Pin
Nandakishore G N18-Feb-15 20:25
professionalNandakishore G N18-Feb-15 20:25 

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

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