Pubsub - hello world example

I know others have posted requesting a simple concrete sample application (source code) illustrating the basics of pubsub in openfire but I’ve not seen any responses/pointers. In fact, I’ve not seen any examples / pointers outside the openfire community either (that doesn’t mean an example doesn’t exist).

Was hoping that someone can provide such a pointer; I’m sure I’m not the only one that would be interested to get a response.

Thanks in advance,

Dave

This is a simple example using the pubsub extensions I wrote here

Create and publish

ConfigureForm form = new ConfigureForm(FormType.submit);
form.setPersistentItems(false);
form.setDeliverPayloads(true);
form.setAccessModel(AccessModel.open);

PubSubManager manager = new PubSubManager(connection, “pubsub.my.openfire.server”);

Node myNode = manager.createNode(“TestNode”, form);

SimplePayload payload = new SimplePayload(“book”,“pubsub:test:book”, “Lord of the Rings”);

Item item = new Item(itemId, payload);

// Required to recieve the events being published

myNode.addItemEventListener(myEventHandler);

// Publish item

myNode.publish(item);

**Get node and listen (different user)
**

Node eventNode = manager.getNode(“TestNode”);

eventNode.addItemEventListener(myEventHandler);

eventNode.subscribe(“myJID”);

THANK YOU!!! for posting this.

Best,

Dave

I’m using smackx-pubsub 0.4.

When I leave the “itemId” argument as null, letting the XMPP server to generated it for me:

Item item = new Item(null, payload);

The generated IQ stanza has incorrectly inserted additional “>” character in between and tags.

OK, I will check that out. I may have missed that particular use case in my tests. I am currently on vacation though so it won’t be updated for a couple of weeks. I would suggest generating the id’s yourself in the meantime.

Hi,

Thanks for the hello world example. I tried it out, I was able to create the node and publish items to the node, and the notification was able to work if the subscriber is the publisher itself.

This works because automatically the publisher automatically becomes a subscriber (I think part of the XEP-0060 specs says so).

If the subscriber is another Jabber contact, then there would need to be some extra code, I think, calling the subscribe(…) method of the Node class. Please correct me if I’m wrong.

You are correct. I have updated the sample code to reflect the publisher vs. a consumer receiving events.

This has now been fixed in the 0.6 version available here in the original posting. There are a couple of other changes as well since a LeafNode and CollectionNode have been introduced and a couple of methods have been removed from the PubSubManager. I will update the javadoc there as well ASAP.

Thanks for the code! Quite useful!

Now I have a question. In your examples (and from what I’ve been able to see, in the API) you can add ItemEventListeners to single nodes. Is there a way to add a single ItemEventListener to all of the nodes that a user is subscribed to, or do we have to add them to each node separately?

The API is strictly node driven, so you have to do it by node.

Of course, you can simply register your own packet listener through the regular Smack API with an appropriate filter to accomplish the same thing. The pubsub API simply provides some conveniences beyond the more generic approach, but doesn’t stop you from using it as well.

hellow

can you tell me where and how to integrate the code to create a node in openfire

thanks in advance

Just follow the instructions in Smack for how to create a connection to a server from a client application. That connection is then used as documented in the example.

The example code is pretty much complete for creating a node, subscribing and registering a listener. The only part not shown is the creation of the connection, since it is standard Smack usage.

ok rcollier

is that is the code that i added at the beginning of file PubSubManager.java

// Create a connection to the XMPP server.

XMPPConnection con = new XMPPConnection(“openfire”);

// Connect to the server

con.connect();

// Most servers require you to login before performing other tasks.

con.login("", “”);

ok rcollier

is that is the code that i added at the beginning of file PubSubManager.java

// Create a connection to the XMPP server.
XMPPConnection con = new XMPPConnection(“openfire”);
// Connect to the server
con.connect();
// Most servers require you to login before performing other tasks.
con.login("", “”);

Awesome work!! thanks so much for sharing.

Can you please explain in your constructor

PubSubManager manager = new PubSubManager(connection, “pubsub.my.openfire.server”);

  1. what is “pubsub.my.openfire.server” and what exactly I need to replace it with?

  2. I am assuming, the subscribers can use the Service Discovery feature in

http://www.igniterealtime.org/builds/smack/docs/latest/documentation/extensions/ index.html

smackx.jar for discovering node info, and accordingly then use your API to subscribe to these nodes?

i.e.

// Obtain the ServiceDiscoveryManager associated with my XMPPConnection
      ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection);
     
      // Get the information of a given XMPP entity
      // This example gets the information of a conference room
      DiscoverInfo discoInfo = discoManager.discoverInfo("balconyscene@plays.shakespeare.lit");

      // Get the discovered identities of the remote XMPP entity
      Iterator it = discoInfo.getIdentities();
  1. pubsub.my.openfire.server = pubsub. + hostname used to create connection. Pubsub is the default service name, just like conference is used to address that service. For example, there is an XMPP server at igniterealtime.org. The conference service is addressed as conference.igniterealtime.org and the pubsub service is pubsub.igniterealtime.org (I am not implying that you can access the pubsub.igniterealtime.org).

  2. Yes. In fact, PubsubManager.discoverNodes(), **PubSubManager.getSupportedFeatures() **and Node.discoverInfo() are all convenience methods for obtaining this information.

1.can someone post workabout example of helloworld with “xmppconnection” ?

2.is there any configuration need to add/edit at openfire server?

  1. is openfire port 5222 open by default for the connection?

problem solved. changed PubSubManager manager = new PubSubManager(connection, “myserver”);

after published. may i know how to check the message using spark ? i add the user@domain/theNode correct? i cannot received any notification.

I don’t think Spark has any support for pubsub, so you will not be able to subscribe for the notifications.