zozelfelfo
zozelfelfo

Reputation: 3776

XMPP Pubsub problems subscribe and publish using smack

I am trying to use Pubsub module into my application and right now I am trying to run some publish / subscription test, with no result so far.

In the server side I am using ejabberd 2.1.11 with pubsub installed (or at least that's what it seems)

mod_pubsub_odbc:     [{access_createnode, pubsub_createnode},
{pep_sendlast_offline, false},
{last_item_cache, false},
{plugins, ["flat", "hometree", "pep"]}]

In the client side I have been able to create nodes, but I am not able to subscribe to them. Here is my code

XMPPConnection mycon = new XMPPTCPConnection(connectionConfiguration);

mycon.connect();
mycon.login("phantom_notifier", "phantom_notifier");

PubSubManager mgr = new PubSubManager(mycon);
LeafNode leaf = mgr.getNode("testNode2");

leaf.addItemEventListener(new ItemEventListener()
{
    @Override
    public void handlePublishedItems(ItemPublishEvent items) {
        //TODO
    }
});
leaf.subscribe(mycon.getUser());

After running this code I get always the following exception

java.io.EOFException: no more data available - expected end tag </stream:stream> to close start tag <stream:stream> from line 1, parser stopped on END_TAG seen ...</x></query></iq>... @1:3734
at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:3035)
at org.xmlpull.mxp1.MXParser.more(MXParser.java:3046)
at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1144)
at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)
at org.jivesoftware.smack.tcp.PacketReader.parsePackets(PacketReader.java:279)
at org.jivesoftware.smack.tcp.PacketReader.access$000(PacketReader.java:47)
at org.jivesoftware.smack.tcp.PacketReader$1.run(PacketReader.java:81)

org.jivesoftware.smack.SmackException$NoResponseException
at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:191)
at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:175)
at org.jivesoftware.smackx.pubsub.PubSubManager.sendPubsubPacket(PubSubManager.java:303)
at org.jivesoftware.smackx.pubsub.Node.sendPubsubPacket(Node.java:443)
at org.jivesoftware.smackx.pubsub.Node.subscribe(Node.java:248)
at main.DBChangesReceiver.main(DBChangesReceiver.java:115)

No matter to what node I subscribe...

On the other side every time I try to publish a message I receive no error but checking the log I see that my message was not right. The code I use to publish a message is the following

leaf.publish(new PayloadItem<SimplePayload>("test" + System.currentTimeMillis(),
new SimplePayload("book", "pubsub:test:book",
"test book")));

In the debugger my last sent and received message is the following:

Sent message

<iq id="BT97r-10" to="pubsub.myserver" type="set">
 <pubsub xmlns="http://jabber.org/protocol/pubsub">
   <publish node="testNode2">
    <item id="test1413022577642">test book</item>
  </publish>
</pubsub>

Received message

<iq id="BT97r-10" to="phantom_notifier@myserver/Smack" from="pubsub.myserver" type="error">
 <pubsub xmlns="http://jabber.org/protocol/pubsub">
  <publish xmlns="http://jabber.org/protocol/pubsub">
    <item>test book</item>
  </publish>
 </pubsub>
 <error type="modify">
   <bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
   <invalid-payload xmlns="http://jabber.org/protocol/pubsub#errors"/>
  </error>
</iq>

Right now I have no clue about that invalid payload, cheking the documentation on the internet I see no big differences.

EDIT

After @Flow suggestions the package sent to the server is the following:

<iq id="XIaTx-11" to="pubsub.myserver" type="set">
 <pubsub xmlns="http://jabber.org/protocol/pubsub">
  <publish node="testNode2">
   <item id="test1413099855673">
     <book xmlns="pubsub:test:book">text book</book>
   </item>
  </publish>
 </pubsub>
</iq>

The server does not send a response as it seems there is some critical error (the same commented as when I try to subscribe):

oct 12, 2014 9:44:15 AM org.jivesoftware.smack.XMPPConnection callConnectionClosedOnErrorListener
Connection closed with error
java.io.EOFException: no more data available - expected end tag </stream:stream> to close start tag <stream:stream> from line 1, parser stopped on END_TAG seen ...</x></query></iq>... @1:3625
at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:3035)
at org.xmlpull.mxp1.MXParser.more(MXParser.java:3046)
at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1144)
at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)
at org.jivesoftware.smack.tcp.PacketReader.parsePackets(PacketReader.java:279)
at org.jivesoftware.smack.tcp.PacketReader.access$000(PacketReader.java:47)
at org.jivesoftware.smack.tcp.PacketReader$1.run(PacketReader.java:81)

And here I have the ejabberd error log (hope it is useful)

=ERROR REPORT==== 2014-10-12 09:45:38 ===
** State machine <0.4880.3> terminating
** Last message in was {route,
                    {jid,[],"pubsub.myserver",[],[],
                     "pubsub.myserver",[]},
                    {jid,"phantom_notifier","myserver","Smack",
                     "phantom_notifier","myserver","Smack"},
                    {xmlelement,"iq",
                     [{"type","error"},
                      {"from","pubsub.myserver"},
                      {"id","XIaTx-11"}],
                     [{xmlelement,"pubsub",
                       [{"xmlns","http://jabber.org/protocol/pubsub"}],
                       [{xmlelement,"publish",
                         [{"node","testNode2"}],
                         [{xmlelement,"item",
                           [{"id","test1413099855673"}],
                           [{xmlelement,"book",
                             [{"xmlns","pubsub:test:book"}],
                             [{xmlcdata,<<"text book">>}]}]}]}]},
                      {aborted,no_transaction}]}}


** When State == session_established
**      Data  == {state,
                 {socket_state,gen_tcp,#Port<0.218148>,<0.4879.3>},
                 ejabberd_socket,#Ref<0.0.12.23022>,false,"2115632325",
                 {sasl_state,"jabber","myserver",[],
                     #Fun<ejabberd_c2s.1.30334249>,
                     #Fun<ejabberd_c2s.2.32721014>,
                     #Fun<ejabberd_c2s.3.7052687>,cyrsasl_digest,
                     {state,5,"3654209317","phantom_notifier",[],
                         #Fun<ejabberd_c2s.1.30334249>,
                         #Fun<ejabberd_c2s.3.7052687>,
                         ejabberd_auth_internal,"myserver"}},
                 c2s,c2s_shaper,false,true,false,false,
                 [verify_none,{certfile,"/etc/ejabberd/ejabberd.pem"}],
                 true,
                 {jid,"phantom_notifier","myserver","Smack",
                     "phantom_notifier","myserver","Smack"},
                 "phantom_notifier","myserver5","Smack",
                 {{1413,99937,977476},<0.4880.3>},
                 {pres_t,1},
                 {pres_f,1},
                 {pres_a,1},
                 {pres_i,0},
                 {xmlelement,"presence",
                     [{"id","XIaTx-9"}],
                     [{xmlelement,"status",[],
                          [{xmlcdata,<<"Phantom on the phone">>}]},
                      {xmlelement,"priority",[],[{xmlcdata,<<"42">>}]}]},
                 undefined,
                 {{2014,10,12},{7,45,37}},
                 false,
                 {userlist,none,[],false},
                 c2s,ejabberd_auth_internal,
                 {{84,125,131,61},56127},
                 []}
** Reason for termination =
** {{badxml,
    {xmlelement,"iq",
        [{"from","pubsub.myserver"},
         {"to","phantom_notifier@myserver/Smack"},
         {"type","error"},
         {"id","XIaTx-11"}],
        [{xmlelement,"pubsub",
             [{"xmlns","http://jabber.org/protocol/pubsub"}],
             [{xmlelement,"publish",
                  [{"node","testNode2"}],
                  [{xmlelement,"item",
                       [{"id","test1413099855673"}],
                       [{xmlelement,"book",
                            [{"xmlns","pubsub:test:book"}],
                            [{xmlcdata,<<"text book">>}]}]}]}]},
         {aborted,no_transaction}]},
    {{case_clause,{aborted,no_transaction}},
     [{xml,element_to_string_nocatch,1},
      {xml,'-element_to_string_nocatch/1-lc$^0/1-0-',1},
      {xml,'-element_to_string_nocatch/1-lc$^0/1-0-',1},
      {xml,element_to_string_nocatch,1},
      {xml,element_to_string,1},
      {xml,element_to_binary,1},
      {ejabberd_c2s,send_element,2},
      {ejabberd_c2s,handle_info,3}]}},
[{xml,element_to_string,1},
 {xml,element_to_binary,1},
 {ejabberd_c2s,send_element,2},
 {ejabberd_c2s,handle_info,3},
 {p1_fsm,handle_msg,10},
 {proc_lib,init_p_do_apply,3}]}

The error as we can see is related to the one given in the client, a bad constructed xml element.

Upvotes: 3

Views: 2233

Answers (1)

Flow
Flow

Reputation: 24053

I believe the problem is that item has just text and no enclosing XML element. This is an unfortunate design issue with Smack's SimplePayload class. Basically you want to replace

"test book"

with

"<book xmlns='pubsub:test:book'>text book</book>"

when constructing the SimplePayload

Upvotes: 3

Related Questions