How to get notifications

Hi

I’m tring to dev a xmpp client with smack lib and I met with one problem on pubsub.

According to http://www.igniterealtime.org/builds/smack/docs/latest/documentation/extensions/ index.html , I’ve succeed in creating nodes on service and subscribing nodes.Then according to xep-0060,when Publisher publishes a item on the node,Subscribers should receive event notifications .But on my client ,how can i get the event notification and deal with it?

Thanks in advance, and i am looking forward positively.

Once you have creatednode or getnode you need to subscribe to the node using you JID. Then add a your own instance of a itemeventlistener.

From the Docs

Examples

In this example we can see how to create a listener and register it and then subscribe for messages.

// Create a pubsub manager using an existing Connection

PubSubManager mgr = new PubSubManager(con);

// Get the node

LeafNode node = mgr.getNode(“testNode”);

node.addItemEventListener(new ItemEventCoordinator());

node.subscribe(myJid);

Where the listener is defined like so:

class ItemEventCoordinator implements ItemEventListener
{
@Override
public void handlePublishedItems(ItemPublishEvent items)
{
System.out.println("Item count: " + items.getItems().size());
System.out.println(items);
}
}

William,Thanks for your answer.

As I understand,your code is meant that when user subscribes to the node,the node will send the notification of the latest items to user .But what i want is , a publiser pubs a new item on the node and then the user who subscribe to the node will get the event notification.My confusion is how to get the event notification and deal with it.

No, what William has is exactly what you want.

Once you subscribe, you will typically be sent the last item that was published to the node and any messages published after your subscription. The code node.addItemEventListener(new ItemEventCoordinator()); is how you setup a listener that can handle those messages, so it is logical to put the listener in place before you subscribe so you don’t miss any messages.

If you want the messages that are already on a node (assuming it is persistent) then you have to explicitly get them via ***node.getItems()***.

rcollier,thanks for your reply!!

I have subscribed to the node and added a listener in place before I subscribe.So after a new item is published on the node,service will send notification to subscribers.

But as the subscriber,what should I do to receive the notification and obtain the information i need(such as the information of published item in it)?

Nothing.

That is what the listener was for so it will now get called for each published item. The listener is your published items handler.

@rcollier

i am not be able to receive any published item by subscribers

here is my code

PubSubManager mgr = new PubSubManager(cxmpp,“pubsub.example.com”);

Node node = null;

try {

//get the node

                          node = mgr.getNode("mynode/loc");

           // register listner

node.addItemEventListener(new ItemEventCoordinator());

//subscribe

node.subscribe("james@example.com");

} catch (XMPPException e) {

e.printStackTrace();

}

and the event listener for incoming published items

class ItemEventCoordinator implements ItemEventListener

{

@Override

public void handlePublishedItems(ItemPublishEvent items)

{

System.out.println("Item count: " + items.getItems().size());

System.out.println(items);

}

}

i have used the above code but my ItemEventListener is not able to get called ,i dont know what is the reason. when my node.subscribe("james@example.com"); get called i am getting successfull message from the server which indecates that the JID is subscribed now. but after that i am not able to see any message or published items , not even in a debug mode

so far i am able to create node , publish node and get the node successfully but after subscribing i am not be able to get the items. i read all threads so far but none of them solved my issue , plz help

can anyone please help me over this , i tried different pactched smack library but i am not be able to make it work.

i tried so many threads too but no luck including this thread