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"}));