I am looking at node-xmpp and node-simple-xmpp and I am trying to make a simple client.
Everything works fine, except the disconnect.
I have made the following file after the example of simple-xmpp:
var xmpp = require('simple-xmpp');
xmpp.on('online', function() {
console.log('Yes, I\'m connected!');
xmpp.send('test2@example.com', 'Hello test');
// OK UNTIL HERE, DISCONNECT NOW
});
xmpp.connect({jid: 'test@example.com/webchat', password: 'test', reconnect: 'false'});
But I don't know how to disconnect. I tried to send a stanza with unavailable type:
stanza = new xmpp.Element('presence', {from: 'test@example.com', type: 'unavailable'});
xmpp.conn.send(stanza);
delete xmpp;
This is causing the client to go temporarily offline, but the problem is, it reconnects after a few seconds and keeps sending 'presence' stanza.
I have also tried calling xmpp.conn.end(), which also disconnects but it gives an error afterwards:
node_modules/simple-xmpp/node_modules/node-xmpp/lib/xmpp/connection.js:100
if (!this.socket.writable) {
^
TypeError: Cannot read property 'writable' of undefined
So, what am I doing wrong? I am sure there is an easy way to disconnect.
In the first case, <presence type='unavailable'/>
does not always actually disconnect you; in your server, it looks like it might be, but your client is auto-reconnecting. delete xmpp
is not actually causing your object to be cleaned up, it's just removing it from the local namespace.
In the second case send()
isn't writing your stanza to the underlying socket immediately. If you close the socket with end()
right afterwards, the socket is closed when the write actually happens.
If you add a short timeout after you call send()
, before calling end()
it will work. To make it good, you'll want your library developers to give you a callback when send()
has actually written to the socket.