firewall firewall - 8 months ago 111
Javascript Question

Send a ping to XMPP server in Javascript

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='juliet@capulet.lit/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!

Answer Source

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"}));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download