Skip navigation
19730 Views 40 Replies Latest reply: Sep 17, 2011 8:49 PM by rcollier RSS
dlevine@pivotinc.com Bronze 3 posts since
Apr 29, 2009
Currently Being Moderated

Apr 30, 2009 6:21 AM

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

  • rcollier KeyContributor 600 posts since
    Mar 4, 2009
    Currently Being Moderated
    Aug 7, 2009 6:46 AM (in response to dlevine@pivotinc.com)
    Re: Pubsub - hello world example

    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");

    • williamyeh Bronze 4 posts since
      Jul 20, 2009
      Currently Being Moderated
      Jul 20, 2009 2:37 AM (in response to rcollier)
      Re: Pubsub - hello world example

      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 "&gt;" character in between <item> and <book> tags.

      • rcollier KeyContributor 600 posts since
        Mar 4, 2009
        Currently Being Moderated
        Jul 20, 2009 5:16 AM (in response to williamyeh)
        Re: Pubsub - hello world example
        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.
      • rcollier KeyContributor 600 posts since
        Mar 4, 2009
        Currently Being Moderated
        Aug 7, 2009 8:08 AM (in response to williamyeh)
        Re: Pubsub - hello world example
        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.
    • Joon-Yew Liow Bronze 1 posts since
      Aug 5, 2009
      Currently Being Moderated
      Aug 7, 2009 12:16 AM (in response to rcollier)
      Re: Pubsub - hello world example

      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.

    • mcamou Bronze 30 posts since
      Oct 10, 2006
      Currently Being Moderated
      Sep 10, 2009 12:52 PM (in response to rcollier)
      Re: Pubsub - hello world example

      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?

      • rcollier KeyContributor 600 posts since
        Mar 4, 2009
        Currently Being Moderated
        Sep 10, 2009 2:21 PM (in response to mcamou)
        Re: Pubsub - hello world example

        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.

    • xp1woft1 Bronze 5 posts since
      Mar 3, 2010
      Currently Being Moderated
      Mar 8, 2010 2:45 AM (in response to rcollier)
      Re: Pubsub - hello world example

      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,

    • Disturbed1 Bronze 4 posts since
      Apr 29, 2010
      Currently Being Moderated
      May 2, 2010 8:18 PM (in response to rcollier)
      Re: Pubsub - hello world example

      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

    • Ohmer Bronze 141 posts since
      Aug 26, 2005
      Currently Being Moderated
      Jul 8, 2010 7:45 AM (in response to rcollier)
      Re: Pubsub - hello world example

      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!

    • Sharique Javaid Bronze 2 posts since
      Sep 17, 2011
      Currently Being Moderated
      Sep 17, 2011 9:32 AM (in response to rcollier)
      Re: Pubsub - hello world example

      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!

      • rcollier KeyContributor 600 posts since
        Mar 4, 2009
        Currently Being Moderated
        Sep 17, 2011 8:49 PM (in response to Sharique Javaid)
        Re: Pubsub - hello world example

        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();

                }

  • talsam Bronze 10 posts since
    Sep 10, 2009
    Currently Being Moderated
    Sep 11, 2009 2:28 AM (in response to dlevine@pivotinc.com)
    Re: Pubsub - hello world example

    hellow

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

    thanks in advance

    • rcollier KeyContributor 600 posts since
      Mar 4, 2009
      Currently Being Moderated
      Sep 11, 2009 5:28 AM (in response to talsam)
      Re: Pubsub - hello world example

      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.

      • talsam Bronze 10 posts since
        Sep 10, 2009
        Currently Being Moderated
        Sep 11, 2009 8:43 AM (in response to rcollier)
        Re: Pubsub - hello world example

        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("", "");

      • talsam Bronze 10 posts since
        Sep 10, 2009
        Currently Being Moderated
        Sep 11, 2009 8:43 AM (in response to rcollier)
        Re: Pubsub - hello world example

        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("", "");

  • waterlooguy Bronze 1 posts since
    Sep 13, 2009
    Currently Being Moderated
    Sep 13, 2009 11:24 PM (in response to dlevine@pivotinc.com)
    Re: Pubsub - hello world example

    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();
  • Sharique Javaid Bronze 2 posts since
    Sep 17, 2011
    Currently Being Moderated
    Sep 17, 2011 9:33 AM (in response to dlevine@pivotinc.com)
    Re: Pubsub - hello world example

    Can some one post an updated pubsub hello world example..

     

    Thanks guyz and good wishes

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points