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.
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
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
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.
March 5th, 2009 at 11:39 pm (#)
Hi, I’m just wondering how to run this code. Should I just compile and run it?
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.
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.
March 6th, 2009 at 10:36 pm (#)
Instantiate another message, and send it using the connection?
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)
March 24th, 2009 at 6:33 am (#)
Can you telnet to talk.google.com on port 5222?
May 2nd, 2009 at 8:23 pm (#)
Here a very simple java api that supports chat and voice:
http://www.voiceclearly.com/googleTalkVoiceClearly.jsp
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
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).
July 23rd, 2009 at 1:09 pm (#)
hai,
how to add these class??
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
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.
July 26th, 2010 at 1:39 pm (#)
can this code works
September 3rd, 2010 at 6:12 pm (#)
Thanks
This works really fine
Thanks a lot