Send a ping to XMPP server in Javascript

2.2k views Asked by At

I'm using Openfire as the server for the XMPP. The problem I'm facing is that the user is being kicked out every few minutes although I changed the server settings to "not kick idle users".

I was searching about this problem, and I found from many posts that many people were facing the same problem and they solved it by sending constantly pings to the server.

And then I found the stanza for the ping in XMPP, which is the following:

<iq from='capulet.lit' to='[email protected]/balcony' id='s2c1' type='get'>
  <ping xmlns='urn:xmpp:ping'/>
</iq>

But I'm not able to translate it to Javascript correctly.

I tried the following but it is still not working

setInterval(function(){
            var message = $msg({to: [server hostname] ,
                                    from: [user jid],
                                    "type": "get"})
                                     .c('query', {xmlns: "urn:xmpp:ping"});
                    connection.sendIQ(message);
},50000);

I would greatly appreciate any help.

Thanks!

1

There are 1 answers

1
Kemal Fadillah On BEST ANSWER

You're building a message stanza instead of an IQ one. You need to create an IQ stanza to ping the server. The child element you created inside the stanza is also wrong. The element name should be ping instead of query.

connection.sendIQ($iq({to: server, from: jid, type: "get"}).c('ping', {xmlns: "urn:xmpp:ping"}));