I’ve been trying to get pubsub to work on Openfire and Smack for about a week. Finally I’ve managed to get it working this morning so I’ve decided to document seeing that it quite difficult to get all the info in 1 place. The solutions came from various place but in particular from ignite realtime forum and stackoverflow thread. So many thanks to those folks over there.

Here is my setup
1. Openfire server 3.6.4. Setup a few account for testing. Note: you can use any XMPP server but you have to make sure that the pubsub service is running. One way to do this is to use Pidgin, install the XMPP Service Discovery plugin and introspect the server. You should see something like this.

2. Smack from SVN. The release version does not have support for pubsub viz. there are nice classes like PubSubManager, etc. You will have to construct the pubsub stanza yourself.

Here is the code to create a publish an item

//Create a XMPPConnection
//For debugging
XMPPConnection.DEBUG_ENABLED = true;

ConnectionConfiguration config = new ConnectionConfiguration(HOST, PORT);
XMPPConnection conn = new XMPPConnection(config);
conn.login(USER, PASSWORD);

//Create the topic
ConfigureForm f = new ConfigureForm(FormType.submit);
//Set some params for the topic – according to your requirement

//Note that you need to set the second parameter for openfire. This is the
//same as the pubsub service name
PubSubManager mgr = new PubSubManager(conn, “pubsub.” + HOST);
Node n = mgr.createNode(TOPIC, f);

//Now publish something – See Javadocs
SimplePayload payload = new SimplePayload(“book”,”pubsub:test:book”, “”);

PayloadItem payloadItem = new PayloadItem(null, payload);

If you writing a real app, you would probably not use SimplePlayload but extend from org.jivesoftware.smackx.pubsub.Item.

Finally here is the code to subscribe to a topic

//mgr == PubSubManager
Node n = mgr.getNode(TOPIC);
n.addItemEventListener(new ItemEventListener() {
public void handlePublishedItems(ItemPublishEvent items) {
//Handle items

Good luck!

Technorati Tags: , , , ,


3 Responses to XMPP PubSub

  1. Jaya Krishna says:


    Very well explained. Thanks for the detailed code snippet.

    By the way, could you please help me understand the difference between the terms in this pubsub context like ‘features’, ‘nodes’ and ‘services being offered’.

    My primary question is how the features differ to that of services being offered?

    Actually, I’m writing a client application and I know a feature available in my connected server, just wondering how to get subscribed to the node using this feature and listen to the items being published at the same.

    Any inputs in this regard are highly appreciable.

    Thanks in advance,
    Jaya Krishna

  2. Jaya Krishna says:

    Thanks for your inputs, will take a look at it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: