Just played with Smack’s PubSub extension and it’s working great. PubSub is defined in XEP-0060 and offers tremendous possibilities. It’s basically the same model as JMS API. There are publishers and receivers. Publishers publish their’s payload to node and receivers subscribe to those nodes and receive those payloads. Needless to say, payload can be anything. Scenario told here is just a basic example, XEP-0060 is much more than this. Robustness, scalability and other “hot” topics surrounding JMS are left to implementations, e.g. concrete implementation of Jabber server. Speaking of that, there is no better Jabber server implementation than Openfire – open-source Jabber server written in Java.
Guys that wrote Openfire also wrote Smack – also open-source XMPP client library, and now there is PubSub extension for Smack. It’s fairly new, but as I test it, it does a nice job; although I didn’t test any funky scenarios, all that I tried worked and there were no surprises or hidden gotchas. You can find library here for now, but good resources for starting can also be found on theirs community forum here and here. Working with Smack’s PubSub extension is fairly easy (as with Smack in general). Before I post code, just to list some prerequisites in order to try this – you will need to have Jabber server with PubSub ability (if you don’t have one, I’d be happy to offer you to play on mine) and to create two test accounts on it – one for publishing, one for receiving. Create new project, add smack’s libraries and put this:
public class PublisherTest { public static void main(String[] args) throws XMPPException { // connect as publisher ConnectionConfiguration config = new ConnectionConfiguration("kokanovic.org", 5222); XMPPConnection connection = new XMPPConnection(config); connection.connect(); connection.login("publisher", "123"); // needed first time only to configure node ConfigureForm form = new ConfigureForm(FormType.submit); form.setPersistentItems(false); form.setDeliverPayloads(true); form.setAccessModel(AccessModel.open); // create node (comment second line and uncomment third to just get already created node PubSubManager manager = new PubSubManager(connection, "pubsub.myserver.org"); LeafNode myNode = (LeafNode) manager.createNode("MyNode", form); //Node myNode = manager.getNode("MyNode"); SimplePayload payload = new SimplePayload("event", "pubsub:test:event", "" + "<title>New blog post</title>" + "<desc>Stalker published new blog post</desc>" + ""); // putting null for id means you let server generate id PayloadItem item = new PayloadItem(null, payload); // you could use publish() for asynchronous call myNode.send(item); } }
Receiver is similar, but a bit simpler:
public class ReceiverTest { public static void main(String[] args) throws XMPPException { // connect as receiver ConnectionConfiguration config = new ConnectionConfiguration("kokanovic.org", 5222); XMPPConnection connection = new XMPPConnection(config); connection.connect(); connection.login("receiver", "123"); // start listening for published items PubSubManager manager = new PubSubManager(connection, "pubsub.myserver.org"); Node eventNode = manager.getNode("MyNode"); eventNode.addItemEventListener(new ItemEventListener() { @Override public void handlePublishedItems(ItemPublishEvent items) { for (Item item : items.getItems()) { System.out.println(((PayloadItem)item).getPayload().toXML()); } } }); // you will need this first time only eventNode.subscribe("receiver@myserver.org"); // do not die on me! while(true){ try { Thread.sleep(100); } catch (InterruptedException e) {} } } }
Future of PubSub…well, it depends, and it heavily depends now of Google Wave. Don’t get me wrong, future of XMPP is secured now Google Wave is there (in fact, it was secured with Google Talk:), but future of PubSub will depend of direction Google Wave will take, e.g. will it somehow take over PubSub responsibilities and functionalities. Truth is, PubSub is not nearly utilized as it could be, and my opinion is that it’s because no big player stands behind it, no big player stands behind any implementation – there are just no “enterprisy” component in PubSub. Google Wave aside, I have no doubt that PubSub will be more and more popular, it’s its open nature and great architecture that directs to that conclusion. Also, don’t for a moment think users will ever be aware of PubSub, just as they are not aware now of XMPP protocol while chatting in Google Talk. PubSub will be somewhere down under, happily collaborating with Jabber server, Jabber client libraries and happy developer that uses it. Just think what can be done with it – from writing bots that respond to events, to turning your toaster on and off. Possibilities are endless.
GREAT WORK !! Helped me a lot