Last active
August 29, 2015 14:24
Using jxt-xmpp with node-xmpp-component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var JXT = require('jxt').createRegistry(); | |
var XMPPComponent = require('node-xmpp-component'); | |
JXT.use(require('jxt-xmpp-types')); | |
JXT.use(require('jxt-xmpp')); | |
var Message = JXT.getMessage('component'); | |
var server = new XMPPComponent({ | |
jid: 'component.example.com', | |
password: 'secret', | |
host: 'example.com', | |
port: 5347, | |
reconnect: true | |
}); | |
server.on('online', function _online() { | |
console.log('== Online'); | |
sendMessage({ | |
to: 'user@example.com', | |
from: 'component.example.com', | |
body: 'hiya' | |
}); | |
}); | |
server.on('connect', function _connect() { | |
console.log('== Connected'); | |
}); | |
server.on('disconnect', function _disconnect() { | |
console.log('== Disconnected'); | |
}); | |
server.on('reconnect', function _reconnect() { | |
console.log('== Reconnecting'); | |
}); | |
server.on('offline', function _offline() { | |
console.log('== Offline'); | |
}); | |
server.on('error', function _error(err) { | |
console.error(err); | |
}); | |
process.on('exit', function _exit() { | |
server.end(); | |
}); | |
function sendMessage(data) { | |
var msg = new Message(data); | |
server.send(msg.xml); | |
} | |
// Capture and log out the parsed version of all incoming stanzas | |
server.on('stanza', function _stanza(stanza) { | |
// Attach missing content namespace | |
stanza.attrs.xmlns = 'jabber:component:accept'; | |
var stanza2 = JXT.build(stanza); | |
console.log(JSON.stringify(stanza2)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment