Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / Windows Forms

Google Chat Desktop Application using Jabber.Net

Rate me:
Please Sign up or sign in to vote.
4.83/5 (40 votes)
20 Mar 2009Public Domain1 min read 275K   18.9K   112   73
A simple Google chat ( gtalk ) application using Jabber.Net
Gtalk_Messenger.jpg

Introduction

This is a simple Google chat ( gtalk ) desktop application developed in C#. With this application, we can easily chat with our Gmail contacts. This application serves as a best approach for developers to start with the chat application for Gmail.

Background

This application is developed using a .NET open source library, Jabber.net. This library contains a set of .NET controls for sending and receiving extensible messaging and presence protocol (XMPP), also known as the Jabber.

Using the Code

After installing the Jabber.Net library, you can see the list of Jabber.Net controls in the Visual Studio toolbox. Now, add the JabberClient control to the form and assign the user name and password.

C#
User = txtUserName.Text;
Pwd = txtPassword.Text;
jabberClient1.User = User;
jabberClient1.Server = "gmail.com";
jabberClient1.Password = Pwd;
jabberClient1.AutoRoster = true;

Now, add a RosterTree control to populate the contact list and assign the instance of the roster manager and the presence manager to the roster tree control.

C#
rm = new RosterManager();
rm.Stream = jabberClient1;
rm.AutoSubscribe = true;
rm.AutoAllow = jabber.client.AutoSubscriptionHanding.AllowAll;
rm.OnRosterBegin += new bedrock.ObjectHandler(rm_OnRosterBegin);
rm.OnRosterEnd += new bedrock.ObjectHandler(rm_OnRosterEnd);
rm.OnRosterItem += new RosterItemHandler(rm_OnRosterItem);

The RosterManager will add the contact list to the roster tree and the PresenceManager will add a notification for the contact whether the contact is in online or offline mode.

C#
pm = new PresenceManager();
pm.Stream = jabberClient1;

rosterTree1.RosterManager = rm;
rosterTree1.PresenceManager = pm;

After assigning all of the above, we have to call the connect() method to login with the given credentials and populate the roster tree with the contact list.

C#
jabberClient1.Connect();
jabberClient1.OnAuthenticate += new bedrock.ObjectHandler(jabberClient1_OnAuthenticate);

On double clicking a contact in the contact list will open a chat window, through which the user can send messages using the following function:

Gtalk_chat.jpg

C#
private void SendMessage()
{
	jabber.protocol.client.Message reply = 
		new jabber.protocol.client.Message(_jabberClient.Document);
    if (rtbSendMessage.Text != "\n" && rtbSendMessage.Text != " ")
    {
        reply.Body = rtbSendMessage.Text;
        if (reply.Body != "")
        {
            reply.To = _mailId;
            _jabberClient.Write(reply);
            string sentMsg = _jabberClient.User + " Says : " + 
					rtbSendMessage.Text + "\n";
            AppendConversation(sentMsg);
            rtbSendMessage.Text = "";
        }
    }            
}

The JabberClient is registered with an onMessage event to receive whenever an incoming message arrives. This is handled by the following function:

C#
public void _jabberClient_OnMessage
	(object sender, jabber.protocol.client.Message msg)
{
   if (!this.ReceiveFlag)
   {
       if (msg.From.Bare == this.MailId)
       {
           if (msg.Body != "")
           {
               string receivedMsg = msg.From.User + " Says : " + msg.Body + "\n";
               AppendConversation(receivedMsg);
               msg.Body = "";
           }
       }
   }
}

History

  • Released in March 2009

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionlogin unsuccessful Pin
Member 1306360017-Mar-17 3:07
Member 1306360017-Mar-17 3:07 
QuestionDoes this still work with the new hangouts ? Pin
NikoTanghe19-May-16 8:29
NikoTanghe19-May-16 8:29 
QuestionCan this app be used with another server? Pin
Member 1174653827-Nov-15 16:20
Member 1174653827-Nov-15 16:20 
QuestionAutoreply not working Pin
Member 805727312-Oct-15 21:29
Member 805727312-Oct-15 21:29 
QuestionNeed help setting Subscription to "both" Pin
Member 1047794224-Feb-15 15:25
Member 1047794224-Feb-15 15:25 
Questioncan we send or receive files like messages? Pin
shashank76814-Dec-14 19:25
shashank76814-Dec-14 19:25 
Questionsend images Pin
kazemfallahi137117-Sep-13 3:15
kazemfallahi137117-Sep-13 3:15 
GeneralMy vote of 5 Pin
Member 919827214-Jun-13 21:56
Member 919827214-Jun-13 21:56 
Questionaltering it Pin
Keith Aiden17-May-13 6:16
Keith Aiden17-May-13 6:16 
QuestionThe user name and password login unsuccessful Pin
lanyan_hhb29-Jan-13 15:20
lanyan_hhb29-Jan-13 15:20 
AnswerRe: The user name and password login unsuccessful Pin
Member 1306360018-Mar-17 3:32
Member 1306360018-Mar-17 3:32 
QuestionAny updates? Pin
kiquenet.com18-Jan-13 1:38
professionalkiquenet.com18-Jan-13 1:38 
QuestionJoin Group Chat Pin
tithsochinda9-Jan-13 23:28
tithsochinda9-Jan-13 23:28 
GeneralMy vote of 5 Pin
Ankita M4-Nov-12 23:10
Ankita M4-Nov-12 23:10 
QuestionCan We Get Previously Set Status Message Pin
KarthikeyanThangavel1-Nov-12 23:35
KarthikeyanThangavel1-Nov-12 23:35 
GeneralMy vote of 5 Pin
gaga blues22-Oct-12 4:13
gaga blues22-Oct-12 4:13 
GeneralMy vote of 5 Pin
arunraju1-Aug-12 23:02
arunraju1-Aug-12 23:02 
QuestionDoubt Pin
SIVA111118-Jul-12 0:10
SIVA111118-Jul-12 0:10 
QuestionStatus Pin
Infinity82717-Apr-12 11:21
Infinity82717-Apr-12 11:21 
AnswerRe: Status Pin
naoufal zerai18-Aug-12 19:07
naoufal zerai18-Aug-12 19:07 
GeneralRe: Status Pin
Infinity82719-Aug-12 10:59
Infinity82719-Aug-12 10:59 
GeneralRe: Status Pin
naoufal zerai24-Aug-12 11:28
naoufal zerai24-Aug-12 11:28 
GeneralRe: Status Pin
Infinity82724-Aug-12 14:41
Infinity82724-Aug-12 14:41 
GeneralRe: Status Pin
naoufal zerai30-Aug-12 3:46
naoufal zerai30-Aug-12 3:46 
GeneralRe: Status Pin
Infinity82730-Aug-12 11:33
Infinity82730-Aug-12 11:33 

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.