Click here to Skip to main content
15,881,882 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 276.2K   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

 
QuestionHow to get offline messges functionality Pin
Arif-Imran29-Mar-11 22:08
Arif-Imran29-Mar-11 22:08 
AnswerRe: How to get offline messges functionality Pin
NetDave30-Mar-11 10:27
NetDave30-Mar-11 10:27 
GeneralRe: How to get offline messges functionality Pin
Arif-Imran30-Mar-11 20:21
Arif-Imran30-Mar-11 20:21 
QuestionWorks in default : Having Issues connecting to OpenFire Pin
Member 779629929-Mar-11 7:59
Member 779629929-Mar-11 7:59 
GeneralWorking like a charm Pin
Tae-Sung22-Feb-11 10:22
Tae-Sung22-Feb-11 10:22 
GeneralCode is understanable. Pin
garysprice12-Aug-10 22:05
garysprice12-Aug-10 22:05 
GeneralMy vote of 1 Pin
sunilkumarmano7-Jul-10 3:20
sunilkumarmano7-Jul-10 3:20 
GeneralRe: My vote of 1 Pin
LloydA11112-Dec-10 7:24
LloydA11112-Dec-10 7:24 
sunilkumarmano wrote:
nice concept..


This is not a valid reason to down vote someone. Idiot.


See if you can crack this: fb29a481781fe9b3fb8de57cda45fbef


The unofficial awesome history of Code Project's Bob!

"People demand freedom of speech to make up for the freedom of thought which they avoid."

GeneralUsing Openfire in localmachine Pin
Christatos30-Jun-10 0:28
Christatos30-Jun-10 0:28 
GeneralCreate new account Pin
srdusad19-Apr-10 2:16
srdusad19-Apr-10 2:16 
Generalabout adding contacts option in gtalk application Pin
nitin sanap8-Mar-10 19:06
nitin sanap8-Mar-10 19:06 
GeneralGoogle Chat Application for Windows Mobile using Jabber.Net Pin
jaiswalrahul26-Jan-10 20:40
jaiswalrahul26-Jan-10 20:40 
GeneralHI i need a local chet app like gmail Pin
Sravani54930-Oct-09 2:35
Sravani54930-Oct-09 2:35 
QuestionNo Contacts are displayed in the Roster Pin
pilla G Patrudu1-Aug-09 1:39
pilla G Patrudu1-Aug-09 1:39 
AnswerRe: No Contacts are displayed in the Roster Pin
$enthi£5-Aug-09 4:07
$enthi£5-Aug-09 4:07 
GeneralComponent for VS 2008 Pin
Anand KG23-Jul-09 19:15
Anand KG23-Jul-09 19:15 
Generalplease help Pin
deeps108-Jul-09 0:33
deeps108-Jul-09 0:33 
AnswerRe: please help Pin
$enthi£5-Aug-09 4:06
$enthi£5-Aug-09 4:06 
QuestionChat Group? Pin
i-developer of Istanbul12-Jun-09 1:20
i-developer of Istanbul12-Jun-09 1:20 
AnswerRe: Chat Group? Pin
$enthi£5-Aug-09 4:04
$enthi£5-Aug-09 4:04 
GeneralPlease Help Pin
Member 28960421-Jun-09 21:11
Member 28960421-Jun-09 21:11 
GeneralRe: Please Help Pin
i-developer of Istanbul8-Jun-09 19:56
i-developer of Istanbul8-Jun-09 19:56 
GeneralRe: Please Help Pin
i-developer of Istanbul10-Jun-09 0:38
i-developer of Istanbul10-Jun-09 0:38 
GeneralRe: Please Help Pin
Member 289604211-Jun-09 3:51
Member 289604211-Jun-09 3:51 
GeneralRe: Please Help Pin
i-developer of Istanbul12-Jun-09 1:02
i-developer of Istanbul12-Jun-09 1:02 

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.