Apr 30, 2009 6:21 AM
Pubsub - hello world example
-
Like (0)
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", "<book xmlns='pubsub:test:book'><title>Lord of the Rings</title></book>");
Item<SimplePayload> item = new Item<SimplePayload>(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
Hi Guys
I am new to J2ME.I think it is stupid question where to use this pub sub hello world example code..
please help me
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<SimplePayload> item = new Item<SimplePayload>(null, payload);
The generated IQ stanza has incorrectly inserted additional ">" character in between <item> and <book> tags.
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.
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.
hello,folks
I have a packet ,(
send:<iq id="V2oVY-12" to="conference.zq" type="get"><query xmlns="http://jabber.org/protocol/disco#items"></query></iq>
and receive:
<iq type="result" id="V2oVY-12" from="conference.zq" to="admin@zq/zq">
<query xmlns="http://jabber.org/protocol/disco#items">
<item jid="sh@conference.zq" name="fun"/>
<item jid="test@conference.zq" name="news"/>
</query>
</iq> )
I want to Publish-Subscribe this packet.If item is changed (e.g. new create ,update or delete),openfire server notified me that ! How can I use smack pubsub ?
Thanks in advance,
this is maybe a bit stupid question, but I have a problem with the line
Item<SimplePayload> item = new Item<SimplePayload>(itemId, payload);
Eclipse gives me this error: The type Item is not generic; it cannot be parameterized with arguments <SimplePayload>
My JRE is 1.16 and my SDK is 1.6, so my Java supports generics, but I don't know what's the problem here.
please help
thanks in advance
I'm using smack api for android - asmack, but when I use smackx-pubsub.0.6 I get no error.
solved it, hopefuly, sry for bothering
Change Item to PayloadItem
Item is not generic, but it's subclass which supports payloads is.
I'm getting an error at this exact line:
for (PayloadItem<SimplePayload> item : e.getItems()) {
Error:
-----------
incompatible types
found : java.lang.Object
required: org.jivesoftware.smackx.pubsub.PayloadItem<org.jivesoftware.smackx.pubsub.Simpl ePayload>
Hello,
I'm trying you example code but this doesn't work. I get 2 errors.
The first error is with this line I get "type org.jivesoftware.smackx.pubsub.Item does not take parameterstype org.jivesoftware.smackx.pubsub.Item does not take parameters" :
Item<SimplePayload> item = new Item<SimplePayload>(itemId, payload);
The second problems is with this line :
myNode.addItemEventListener(myEventHandler);
Where do you create "myEventHandler" ? I don't see it in your example.
Thanks for the help!
The first error is because the code has changed since the example was written. Try PayloadItem instead of Item.
The second is left as an exercise for the reader
Just look at the API and check the documentation, it's a pretty interface.
Yes after re-reading all the messages again I finally found I should use PayloadItem.
Now I have a problem with :
myNode.publish(item);
The method publish doesn't seem to exists (I get "cannot find symbol"). There is a new method to send the item to the pubsub service?
Thanks!
Any help?
Thanks.
You need to look at the documentation as this example is becoming rather dated. The Node is now a LeafNode.
Hii,
This page has been very helpful for me.
I am unable to extract the data from the item recieved.
Can u give me a link to a pubusb parser to extract the data from the item recieved.
Still not able to successfuly create an item to send.
Thanks in advance!
This would be the default case if you have included a payload.
public void handlePublishedItems(ItemPublishEvent<T> items)
{
PayloadItem<SimplePayload> item = items.getItems().get(0);
SimplePayload payload = item.getPayload();
String payloadData = payload.toXML();
}
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?
3. 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.
any hello world how to subscribe to the node?
It's in my Hello World example on this page, second item posted.
after submmited "a book" to node "testnode", for subsequent time, cannot submit to the same node because of error 409? is this normal? in this case, how to submit multiple "books" to same node" so that user will receive update ?
I am not sure what that error is. Try running it with the debug console (VM arg -Dsmack.debugEnabled=true and include smackx-debug.jar) so you can see the full error message. It will have a more specific reason for failure.
You should be able to send multiple items to the node.
this is my debug log
<iq id="HRC83-0" to="userx/dd1a53b3" type="result"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>admin@userx/Smack</jid></bind></i q>
<iq id="HRC83-1" to="admin@userx/Smack" type="result"></iq>
<iq id="HRC83-2" to="admin@userx/Smack" type="result"><query xmlns="jabber:iq:roster"><item jid="test" name="test" subscription="none"></item><item jid="test@userx" subscription="from"></item></query></iq>
<iq id="HRC83-4" to="admin@userx/Smack" from="userx" type="error"><pubsub xmlns="http://jabber.org/protocol/pubsub"><create node='ghost'/><configure xmlns="http://jabber.org/protocol/pubsub"><value>0</value></configure></pubsub><error code="409" type="CANCEL"><conflict xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>
Looks like you are trying to create a node "ghost" that already exists. Are you running the same sample code repeatedly, including creating the node? The error is not occuring in publishing.
correct. i run the publishing test application repeatly . if i change Node myNode = manager.createNode("ghost", form); to Node myNode =manager.getNode("ghost") ; for subsequent time, i get error 404 node not found instead
if anybody have solution for Item(node) not found error, Please post the solution to resolvethat problem.
Can some one post an updated pubsub hello world example..
Thanks guyz and good wishes