About XMPP
The Extensible Messaging and Presence Protocol (XMPP) is an open technology for real-time communication, which powers a wide range of applications including instant messaging, presence, multi-party chat, voice and video calls, collaboration, lightweight middleware, content syndication, and generalized routing of XML data.
All offices now days use some kind of instant messaging system to keep in contact with members of staff & clients. In our Office we use Jabber & Pidgin.
Using the Smack API we can build a Java application which works in the same way as these popular XMPP clients. This opens up all kinds of possibilities. I am currently working on a bot which will take incoming commands from anyone in the office and in turn perform tasks such as querying a database and returning information.Originally Posted by igniterealtime.org
To begin, download the Smack API from here:
Ignite Realtime: Smack API
Once you have the Smack API downloaded to your computer. Extract all the .jar files.
You now need to include these jar files into your project.
If you use Eclipse you can follow these instructions:
Right click your project > Properties > Java Build Path > Add External Jars.
Now we have the Smack API included within our project, we can start to use the API.
Create a new class called JabberSmackAPI and add the below code. Make sure you update the parts in bold.
import java.util.*; import java.io.*; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.Roster; import org.jivesoftware.smack.RosterEntry; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Message; public class JabberSmackAPI implements MessageListener{ XMPPConnection connection; public void login(String userName, String password) throws XMPPException { ConnectionConfiguration config = new ConnectionConfiguration("im.server.here",5222, "Work"); connection = new XMPPConnection(config); connection.connect(); connection.login(userName, password); } public void sendMessage(String message, String to) throws XMPPException { Chat chat = connection.getChatManager().createChat(to, this); chat.sendMessage(message); } public void displayBuddyList() { Roster roster = connection.getRoster(); Collection<RosterEntry> entries = roster.getEntries(); System.out.println("\n\n" + entries.size() + " buddy(ies):"); for(RosterEntry r:entries) { System.out.println(r.getUser()); } } public void disconnect() { connection.disconnect(); } public void processMessage(Chat chat, Message message) { if(message.getType() == Message.Type.chat) System.out.println(chat.getParticipant() + " says: " + message.getBody()); } public static void main(String args[]) throws XMPPException, IOException { // declare variables JabberSmackAPI c = new JabberSmackAPI(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String msg; // turn on the enhanced debugger XMPPConnection.DEBUG_ENABLED = true; // Enter your login information here c.login("[B]username[/B]", "[B]password[/B]"); c.displayBuddyList(); System.out.println("-----"); System.out.println("Who do you want to talk to? - Type contacts full email address:"); String talkTo = br.readLine(); System.out.println("-----"); System.out.println("All messages will be sent to " + talkTo); System.out.println("Enter your message in the console:"); System.out.println("-----\n"); while( !(msg=br.readLine()).equals("bye")) { c.sendMessage(msg, talkTo); } c.disconnect(); System.exit(0); } }
When you run this code the Smack Debug window will show.
For more information on what this does and how to use it, see:
Smack: Debugging - Jive Software
The code will then print a list of available contacts. You can select who to talk to and start sending messages to that person.
Here is an example console output:
This is a very simple example of how to use the Smack API. It's a good base for writing your own XMPP client.4 buddy(ies):
andyf@im.javaprogrammingforums.com
pault@im.javaprogrammingforums.com
gregd@im.javaprogrammingforums.com
peterm@im.javaprogrammingforums.com
-----
Who do you want to talk to?
kierond@im.javaprogrammingforums.com
-----
All messages will be sent to kierond@im.javaprogrammingforums.com
Enter your message in the console:
-----
Hello kieron!
For help and a more in depth explanation, see the documentation that comes with the Smack API.