Andrej Koelewijn

  • Home
  • About
  • Publications
  • Departments
    • agile
    • architecture
    • cloud
    • java
    • mobile
    • open standards
    • oracle
    • oss
    • other
    • soa
    • software development
    • tablet
    • Uncategorized
    • web
  • Subscribe via RSS

Using google talk from java example

December 30th, 2008  |  Published in java, open standards, oss  |  42 Comments

Sending and receiving messages to and from google talk accounts is actually very easy. Google uses the xmpp (jabber) protocol. The Smack library enables you to use the xmpp protocol in java.

I was actually trying to use xmpp in apache camel, but couldn’t get it working with google accounts, just with other accounts, so i decided to do some basic smack testing. Smack doesn’t have any problems with google accounts. Here’s a basic example i wrote to test it


// connect to gtalk server
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(connConfig);
connection.connect();

// login with username and password
connection.login("camel.test.1", "secret");

// set presence status info
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);

// send a message to somebody
Message msg = new Message("camel.test.2@gmail.com", Message.Type.chat);
msg.setBody("hello");
connection.sendPacket(msg);

// receive msg
PacketListener pl = new PacketListener() {
@Override
public void processPacket(Packet p) {
System.out.println(p.getFrom() + ": " + p.toString());
if (p instanceof Message) {
Message msg = (Message) p;
System.out.println(msg.getFrom() + ": " + msg.getBody());
}
}
};
connection.addPacketListener(pl, null);

// wait for user to end program
System.in.read();

// set presence status to unavailable
presence = new Presence(Presence.Type.unavailable);
connection.sendPacket(presence);

This is a very basic example, you’d normally create a chat session, and listen for messages part of the chat. More info on the smack site.

  • http://janstey.blogspot.com/ Jon Anstey

    Andrej,

    Cool example. I just fixed up a few things in Camel’s XMPP component so it should be fine now. See an example at the bottom of this page http://cwiki.apache.org/confluence/display/CAMEL/XMPP

    BTW you can send any other issues you have to the user forum http://activemq.apache.org/camel/mailing-lists.html, we’d be more than happy to help.

    Cheers,
    Jon

  • http://janstey.blogspot.com/ Jon Anstey

    Andrej,

    Cool example. I just fixed up a few things in Camel’s XMPP component so it should be fine now. See an example at the bottom of this page http://cwiki.apache.org/confluence/display/CAMEL/XMPP

    BTW you can send any other issues you have to the user forum http://activemq.apache.org/camel/mailing-lists.html, we’d be more than happy to help.

    Cheers,
    Jon

  • http://janstey.blogspot.com/ Jon Anstey

    Andrej,

    I just fixed a few things in Camel’s XMPP component, should work with Google Talk now. Check out a simple example at the bottom of this page http://cwiki.apache.org/confluence/display/CAMEL/XMPP

    By the way, if you have issues in the future, try the Camel mailing lists – we’re usually very responsive. :)

    Happy new year,
    Jon

  • http://janstey.blogspot.com/ Jon Anstey

    Andrej,

    I just fixed a few things in Camel’s XMPP component, should work with Google Talk now. Check out a simple example at the bottom of this page http://cwiki.apache.org/confluence/display/CAMEL/XMPP

    By the way, if you have issues in the future, try the Camel mailing lists – we’re usually very responsive. :)

    Happy new year,
    Jon

  • http://www.andrejkoelewijn.com/ akoelewijn

    Seems the apache people have already fixed the google gtalk problem in apache camel. Details here: CAMEL-1209: camel-xmpp to work with google chat.

  • http://www.andrejkoelewijn.com/ akoelewijn

    Seems the apache people have already fixed the google gtalk problem in apache camel. Details here: CAMEL-1209: camel-xmpp to work with google chat.

  • Ashley

    Hi, I’m just wondering how to run this code. Should I just compile and run it?

  • Ashley

    Hi, I’m just wondering how to run this code. Should I just compile and run it?

  • http://www.andrejkoelewijn.com/ akoelewijn

    You need to put it in a java class, with at least a main method. And you need to add some import statements. Then you can compile and run it.

  • http://www.andrejkoelewijn.com/ akoelewijn

    You need to put it in a java class, with at least a main method. And you need to add some import statements. Then you can compile and run it.

  • Ashley

    With this :

    # Message msg = new Message(“camel.test.2@gmail.com”, Message.Type.chat);
    # msg.setBody(“hello”);
    # connection.sendPacket(msg);

    You can only send an initially hard-coded message which is “hello” while we can keep receiving message.

    How do you change this code so that we can send more than 1 message?

    Thanks.

  • Ashley

    With this :

    # Message msg = new Message(“camel.test.2@gmail.com”, Message.Type.chat);
    # msg.setBody(“hello”);
    # connection.sendPacket(msg);

    You can only send an initially hard-coded message which is “hello” while we can keep receiving message.

    How do you change this code so that we can send more than 1 message?

    Thanks.

  • http://www.andrejkoelewijn.com/ akoelewijn

    Instantiate another message, and send it using the connection?

  • http://www.andrejkoelewijn.com/ akoelewijn

    Instantiate another message, and send it using the connection?

  • Prosenjeet

    I tried using the code snippet but unfortunately it failed with an exception. I am using this code from my office PC in which I have got a direct connection to the Internet. Also, I can ping ‘talk.google.com’ from my machine.
    Please let me know what might have gone wrong. Here’s the whole stack trace.

    XMPPError connecting to talk.google.com:5222.: remote-server-error(502) XMPPError connecting to talk.google.com:5222.
    — caused by: java.net.ConnectException: Connection timed out
    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:900)
    at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1415)
    at com.jeet.test.SampleJabberUtil.login(SampleJabberUtil.java:23)
    at com.jeet.test.SampleJabberUtil.main(SampleJabberUtil.java:62)
    Nested Exception:
    java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.Socket.connect(Socket.java:507)
    at java.net.Socket.connect(Socket.java:457)
    at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:28)
    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:888)
    at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1415)
    at com.jeet.test.SampleJabberUtil.login(SampleJabberUtil.java:23)
    at com.jeet.test.SampleJabberUtil.main(SampleJabberUtil.java:62)

  • Prosenjeet

    I tried using the code snippet but unfortunately it failed with an exception. I am using this code from my office PC in which I have got a direct connection to the Internet. Also, I can ping ‘talk.google.com’ from my machine.
    Please let me know what might have gone wrong. Here’s the whole stack trace.

    XMPPError connecting to talk.google.com:5222.: remote-server-error(502) XMPPError connecting to talk.google.com:5222.
    — caused by: java.net.ConnectException: Connection timed out
    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:900)
    at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1415)
    at com.jeet.test.SampleJabberUtil.login(SampleJabberUtil.java:23)
    at com.jeet.test.SampleJabberUtil.main(SampleJabberUtil.java:62)
    Nested Exception:
    java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.Socket.connect(Socket.java:507)
    at java.net.Socket.connect(Socket.java:457)
    at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:28)
    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:888)
    at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1415)
    at com.jeet.test.SampleJabberUtil.login(SampleJabberUtil.java:23)
    at com.jeet.test.SampleJabberUtil.main(SampleJabberUtil.java:62)

  • http://www.andrejkoelewijn.com/ Andrej

    Can you telnet to talk.google.com on port 5222?

  • http://www.andrejkoelewijn.com/ Andrej

    Can you telnet to talk.google.com on port 5222?

  • http://www.voiceclearly.com/googleTalkVoiceClearly.jsp Thomas

    Here a very simple java api that supports chat and voice:

    http://www.voiceclearly.com/googleTalkVoiceClearly.jsp

  • http://www.voiceclearly.com/googleTalkVoiceClearly.jsp Thomas

    Here a very simple java api that supports chat and voice:

    http://www.voiceclearly.com/googleTalkVoiceClearly.jsp

  • memento

    Hello Andrej,

    nice example especially what i need. But i cant get it worked and i have no idea where the problem is. Maybe the new version of Smack (3.0.1) is buggy.

    Output:

    SASL authentication failed using mechanism PLAIN:
    at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:325)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
    at ConnectivityTest.main(ConnectivityTest.java:22)

    What do you think?

    With regards,

    memento

  • memento

    Hello Andrej,

    nice example especially what i need. But i cant get it worked and i have no idea where the problem is. Maybe the new version of Smack (3.0.1) is buggy.

    Output:

    SASL authentication failed using mechanism PLAIN:
    at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:325)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
    at ConnectivityTest.main(ConnectivityTest.java:22)

    What do you think?

    With regards,

    memento

  • http://bvirtual.nl/ beeman

    @memento, for me the solution was to add @gmail.com to the username in the line where it logs in (line 07 in the example above).

  • http://bvirtual.nl/ beeman

    @memento, for me the solution was to add @gmail.com to the username in the line where it logs in (line 07 in the example above).

  • shanalal

    hai,
    how to add these class??

  • shanalal

    hai,
    how to add these class??

  • amit dubey

    Hi, very nice article with basic information. Thank you

    I would like to discuss a problem here which I am facing with my own Gtalk bot, which is working fine sometimes however It has a bug, I think that is from gtalk server. well situation is like this, when I connect to gtalk server and start processing services for roster users, things go fine until n unless bot becomes idle for few minutes (if no roster user is pinging bot for some time) then google server reconnects this bot (which they do for human users also periodically) bot gets reconnected with previous set presence apart from status message, and subscription mode (auto accept all) also stops working after that. so currently I am try to handle errors as said – displaying status message again and auto accepting roster subscription.

    I am using java and smack 3.1 library. I hope you have some info on this. Thank you

  • amit dubey

    Hi, very nice article with basic information. Thank you

    I would like to discuss a problem here which I am facing with my own Gtalk bot, which is working fine sometimes however It has a bug, I think that is from gtalk server. well situation is like this, when I connect to gtalk server and start processing services for roster users, things go fine until n unless bot becomes idle for few minutes (if no roster user is pinging bot for some time) then google server reconnects this bot (which they do for human users also periodically) bot gets reconnected with previous set presence apart from status message, and subscription mode (auto accept all) also stops working after that. so currently I am try to handle errors as said – displaying status message again and auto accepting roster subscription.

    I am using java and smack 3.1 library. I hope you have some info on this. Thank you

  • jaimin

    hi thanks for this wonderful post.
    But i would like to also have group chat facility so can u please post the source code or demo for the group chat as similar as above using XMPP.

  • jaimin

    hi thanks for this wonderful post.
    But i would like to also have group chat facility so can u please post the source code or demo for the group chat as similar as above using XMPP.

  • harish

    can this code works

  • Buddhika

    Thanks
    This works really fine
    Thanks a lot

  • Sbhukar

    You are behind a firewall which is not allowing you to connect to XMPP server

  • Bala Softtouch

    U R ROCKING MAN……………………..
    this code works fine……………….

  • Farid

    Great,

    Is any GWT version too?

    Regards.

  • Farid

    Great,

    Is any GWT version too?

    Regards.

  • shooding

    You should test your local port 5222 to see if it’s open. If there’s no any firewall between you and talk.google.com, you can try add an exception of port 5222 on your PC’s firewall

  • Perfectvijay

    hey i tried this example but am getting this error:

    Exception in thread “main” XMPPError connecting to talk.google.com:5222.: remote-server-error(502) XMPPError connecting to talk.google.com:5222.
      — caused by: java.net.ConnectException: Connection timed out: connect
        at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:524)
        at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:953)
        at stest.main(stest.java:22)
    Nested Exception:
    java.net.ConnectException: Connection timed out: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(Unknown Source)
        at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:28)
        at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:512)
        at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:953)
        at stest.main(stest.java:22)
     

  • richard

    nice tutorials.
    tips. 
    1) make sure you have necessary jar files in your classpath
    2) connection.login(“camel.test.1″, “secret”); => connection.login(“camel.test.1@gmail.com”, “secret”);
    it works well :)

  • amaan

    XMPPError connecting to talk.google.com:5222.: remote-server-error(502) XMPPError connecting to talk.google.com:5222.
      — caused by: java.net.ConnectException: Connection refused: connect
    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:524)
    at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:953)
    at MyGTalkClient.main(MyGTalkClient.java:26)
    Nested Exception: 
    java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:28)
    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:512)
    at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:953)
    at MyGTalkClient.main(MyGTalkClient.java:26)
    at __SHELL8.run(__SHELL8.java:5)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:774)

  • suneel

    try changing the port 5222 to 80 (or) 443 (or) 5275

  • AMAAN KHAN

    port 80 is working

Tags

activemq agile bi camel css esb google governance grails groovy gtalk html5 internet ipad ivy J2EE java javascript jaxrs jmx jquery lean linkeddata linux maven mule noiv openoffice opensource Open Source oracle osgi oss rdbms rest scrum servicemix soa sql svg tablet web 2.0 XML xmpp yql

Archives

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • June 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Posts

  • Updating a vagrant box
  • Using littleproxy in Mule unit tests
  • Useful site to determine what html5, css3 & svg you can use
  • A Product Owner is a Project Manager
  • Using css webfonts in inkscape

Categories

  • agile
  • architecture
  • cloud
  • java
  • mobile
  • open standards
  • oracle
  • oss
  • other
  • soa
  • software development
  • tablet
  • Uncategorized
  • web

Recent Comments

  • Pcdiggs on Software architecture, PHP and Javascript
  • Een Scrum Product Owner is een Project Leider on A Product Owner is a Project Manager
  • Using css webfonts in inkscape :: Andrej Koelewijn on Create presentations using inkscape
  • Create presentations using inkscape :: Andrej Koelewijn on Presentation: Introduction to Scrum
  • Gebhard Greiter on What is Agile?
Buzz
andrkoel: Utrecht hele dag mist, scheveningen zomerse dag... http://t.co/cDRCJHr9
4:35 PM Nov 10, 2011, comment
andrkoel: RT @stefanvdkamp: Beter filmpje van Garret McNamara in de 30 meter hoge golf. http://t.co/9abiWkYX
9:20 AM Nov 10, 2011, comment
andrkoel: Mmm, een uur voor den haag - leiden lijkt te weinig, ga mijn afspraak niet redden...
7:34 AM Nov 10, 2011, comment
andrkoel: Big Data is Useless without Science http://t.co/2I8EiLsH
6:18 AM Nov 10, 2011, comment
andrkoel: Just tried #vagrant to quickly setup virtualbox development environment. Looks good, although provisioning apache through puppet failed...
10:47 PM Nov 09, 2011, comment
 


©2012 Andrej Koelewijn
Powered by WordPress using the Gridline Lite theme by Graph Paper Press.