Andrej Koelewijn

  • Home
  • About
  • Departments
    • 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  |  17 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.

Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • DZone
  • SphereIt
  • StumbleUpon
  • Technorati
  • LinkedIn
  • HackerNews
  • PDF
  • Digg
  • Facebook
  • FriendFeed
  • Posterous
  • Tumblr
  • Twitter
  • RSS

View Comments

Feed Trackback Address
  1. Jon Anstey says:

    December 31st, 2008 at 7:21 pm (#)

    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

  2. Jon Anstey says:

    December 31st, 2008 at 7:28 pm (#)

    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

  3. akoelewijn says:

    January 3rd, 2009 at 1:46 pm (#)

    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.

  4. Ashley says:

    March 5th, 2009 at 11:39 pm (#)

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

  5. akoelewijn says:

    March 5th, 2009 at 11:46 pm (#)

    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.

  6. Ashley says:

    March 6th, 2009 at 8:21 pm (#)

    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.

  7. akoelewijn says:

    March 6th, 2009 at 10:36 pm (#)

    Instantiate another message, and send it using the connection?

  8. Prosenjeet says:

    March 23rd, 2009 at 12:10 pm (#)

    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)

  9. Andrej says:

    March 24th, 2009 at 6:33 am (#)

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

  10. Thomas says:

    May 2nd, 2009 at 8:23 pm (#)

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

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

  11. memento says:

    May 5th, 2009 at 10:49 pm (#)

    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

  12. beeman says:

    June 3rd, 2009 at 12:02 am (#)

    @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).

  13. shanalal says:

    July 23rd, 2009 at 1:09 pm (#)

    hai,
    how to add these class??

  14. amit dubey says:

    September 29th, 2009 at 5:25 pm (#)

    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

  15. jaimin says:

    November 6th, 2009 at 8:27 am (#)

    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.

  16. harish says:

    July 26th, 2010 at 1:39 pm (#)

    can this code works

  17. Buddhika says:

    September 3rd, 2010 at 6:12 pm (#)

    Thanks
    This works really fine
    Thanks a lot

Leave a Response

blog comments powered by Disqus

Tags

bi bpel camel cep css dsl esb esper google governance grails groovy gtalk html5 innovation internet ipad ivy java javascript jaxrs jersey jigsaw jquery linkeddata linux maven middleware mule noiv openoffice openweb oracle osgi oss plsql rdbms rest soa sql sun tablet web 2.0 xmpp yql

Archives

  • 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

Meta

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

Recent Posts

  • Nice Java Decompiler tool: JD
  • VMware Player: The virtual machine is busy.
  • Adding a maven repository for installing features to ServiceMix
  • Upgrade Apache Camel in ServiceMix to version 2.3.0
  • A composite Rest service using Apache Camel

Categories

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

Recent Comments

  • Buddhika on Using google talk from java example
  • Anonymous on A composite Rest service using Apache Camel
  • Guest on How to find true cause of com.sun.star.uno.RuntimeException?
  • Absent Code attribute in method that is not native or abstract « Gooder Code - web development blog, php, java, asp.net, html, javascript on Absent Code attribute in method that is not native or abstract
  • Rmfume on Oracle best thing that could happen to JavaFX?
Buzz
andrkoel: RT @monkchips: James Governor's Monkchips » Day of The Dead: Web Drives Strong Demand for Java Skills http://monk.ly/d4UlND
9:17 PM Sep 03, 2010, comment
andrkoel: RT @monkchips: In which my business partner @sogrady explains Why You Should Pay Attention to Node.Js http://monk.ly/a4aGIP serverside # ...
3:06 PM Sep 03, 2010, comment
andrkoel: RT @stilkov: http://bit.ly/cDdqgl - AWS Identity and Access Management — I'd hate to have to compete against Amazon's Cloud offerings
8:47 AM Sep 03, 2010, comment
andrkoel: Twitter for ipad is nice, but i still think i need a tool to summarize all info, something like feedly or flipboard is the future
8:36 AM Sep 02, 2010, comment
andrkoel: Trying out the new twitter for ipad... Curious how the panels work.
8:32 AM Sep 02, 2010, comment
 


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