firewall firewall - 3 years ago 330
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'/>

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

I tried the following but it is still not working

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

I would greatly appreciate any help.


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