powered by Jive Software

How to publish Payload to pubsub node in openfire using smack client?

Hi,
How to publish Payload to pubsub node in openfire using smack client
I am getting payload column as

How can I publish object and retrieve it later?

Currently I am doing this.
PubSubManager pm = PubSubManager.getInstance(connection, pubsubJID);
LeafNode senderLeafNode = pm.getNode(nodeID);
senderLeafNode.publish(object);

I’ve written this unit test that creates a node, publishes an item on the node, then scans all items to see if the one that I just added is on there. That seems to cover all of your questions. I hope you can use it as an example.

final String nodename = "sinttest-publish-item-nodename-" + testRunId;
final String needle = "test content " + Math.random();
LeafNode node = pubSubManagerOne.createNode(nodename);
try {
    // Publish a new item.
    node.publish( new PayloadItem<>( GeoLocation.builder().setDescription( needle ).build() ) );

    // Retrieve items and assert that the item that was just published is among them.
    final List<Item> items = node.getItems();
    assertTrue( items.stream().anyMatch( stanza -> stanza.toXML( "" ).toString().contains( needle ) ) );
}
finally {
    pubSubManagerOne.deleteNode( nodename );
}
1 Like

Thanks for the response. It’s working.

I have a Message object in java wgich I am coverting to payload:
PayloadItem topicPayload=new PayloadItem(msg);
and publishing the topicPayload.

This is my payload:

<Message xmlns="http://jabber.org/protocol/pubsub"><sender>username</sender><receiver>receiver</receiver><password>password123</password><body>Message_body</body></Message>

But when I am trying to get the items from the node using node.getItems(), It gives me following exception:

java.lang.IllegalArgumentException: Could not determine namespace from XML payload
at org.jivesoftware.smack.util.StringUtils.requireNotNullOrEmpty(StringUtils.java:449) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.SimplePayload.(SimplePayload.java:61) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.provider.ItemProvider.parse(ItemProvider.java:62) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.provider.ItemProvider.parse(ItemProvider.java:40) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.Provider.parse(Provider.java:43) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.parseExtensionElement(PacketParserUtils.java:918) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.addExtensionElement(PacketParserUtils.java:1001) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.addExtensionElement(PacketParserUtils.java:995) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.EmbeddedExtensionProvider.parse(EmbeddedExtensionProvider.java:101) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.EmbeddedExtensionProvider.parse(EmbeddedExtensionProvider.java:82) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.Provider.parse(Provider.java:43) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.parseExtensionElement(PacketParserUtils.java:918) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.addExtensionElement(PacketParserUtils.java:989) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.addExtensionElement(PacketParserUtils.java:984) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.provider.PubSubProvider.parse(PubSubProvider.java:46) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.provider.PubSubProvider.parse(PubSubProvider.java:34) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.Provider.parse(Provider.java:43) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.parseIQ(PacketParserUtils.java:616) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.parseStanza(PacketParserUtils.java:157) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.AbstractXMPPConnection.parseAndProcessStanza(AbstractXMPPConnection.java:1084) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.tcp.XMPPTCPConnection.access$1300(XMPPTCPConnection.java:155) ~[smack-tcp-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1144) ~[smack-tcp-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$1000(XMPPTCPConnection.java:1092) ~[smack-tcp-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:1112) ~[smack-tcp-4.3.4.jar:4.3.4]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_222]

Why is namespace required after all.

You’ll have to read the documentation for more detailed answers. You could also look at the implementation of the classes that implement PayloadItem for hints at tips.

If you need something simple, then SimplePayload might be useful to you.

1 Like

Because otherwise you would declare elements in a namespace scope you do not own/govern.

Thanks for the response, it was helpful.