Discovering Services

In my previous blog entry, I mentioned that you need to find out if a server supports pubsub. To do that you can use Pidgin’s XMPP Service Discovery Plugin. You can also use Smack to retrieve a list of supported services. Here is the code snippet that will retrieve a list of items/services that a server supports

//Connect and login
XMPPConnection conn = …

ServiceDiscoveryManager mgr = ServiceDiscoveryManager.getInstanceFor(conn);
//Assuming that we are querying jabber.org
DiscoverItems items = mgr.discoverItems(“jabber.org”);
Iterator<DiscoverItems.Item> iter = items.getItems();
while (iter.hasNext()) {
   DiscoverItems.Item i = iter.next();
   System.out.println(i.toXML());
}

This will produce the following from jabber.org

<item jid=”proxy.eu.jabber.org”/>
<item jid=”conference.jabber.org” name=”Multi-User Chat”/>

Once you have this list, you can now use the JID enteries to retrieve other services. For example, notice that jabber.org supports multiuser chat (MUC) with JID conference.jabber.org. We can now use this JID to get a list of rooms, configurations, etc.

Connecting to Google Talk produce the following

Exception in thread “main” feature-not-implemented(501)
   at org.jivesoftware.smackx.ServiceDiscoveryManager.discoverItems(ServiceDiscoveryManager.java:495)
   …

Clearly Google has implemented a no frills server; Google Talk only support 1 to 1 chat and nothing else. BTW to connect to Google talk, use the following code snippet

SASLAuthentication.supportSASLMechanism(“PLAIN”);
ConnectionConfiguration config = new ConnectionConfiguration(
      “talk.google.com”, 5222, “gmail.com”);
XMPPConnection conn = new XMPPConnection(config);
conn.connect();
//Note you MUST have ‘@gmail.com’ to login
conn.login(“gmail_id@gmail.com”, “your password”);

And finally here is the list of services from an instance of openfire running on my notebook

<item jid=”pubsub.batcomputer” name=”Publish-Subscribe service”/>
<item jid=”search.batcomputer” name=”User Search”/>
<item jid=”conference.batcomputer” name=”Public Chatrooms”/>
<item jid=”proxy.batcomputer” name=”Socks 5 Bytestreams Proxy”/>

Notice that pubsub JID which is exactly the service we were trying to connect with PubSubManager.

%d bloggers like this: