Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SASL EXTERNAL Client certificate based authorization using node-xmpp
#!/usr/bin/env node
* Echo Bot - the XMPP Hello World
* Copied from the echo_bot.js example from node-xmpp.
var xmpp = require('../lib/node-xmpp');
var argv = require('optimist').argv;
var fs = require('fs');
var argv = require('optimist')
.usage('Usage: echo_bot_sasl_external.js --jid=<my-jid> [--host=<host>] [--port=<port>] [--legacy_ssl]')
var credentials = {
// These are necessary only if using the client certificate authentication
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
// You might want to put the passphrase of the key file here. You'll be prompted otherwise.
// passphrase: 'YOUR_PASSPHRASE'
var options = { jid: argv.jid, 'credentials': credentials };
if ( options['host'] =;
if (argv.port) options['port'] = argv.port;
if (argv.legacy_ssl) options['legacySSL'] = argv.legacy_ssl;
var cl = new xmpp.Client(options);
cl.on('online', function() {
cl.send(new xmpp.Element('presence', { }).
c('status').t('Happily echoing your <message/> stanzas')
cl.on('stanza', function(stanza) {
if ('message') &&
// Important: never reply to errors!
stanza.attrs.type !== 'error') {
// Swap addresses... = stanza.attrs.from;
delete stanza.attrs.from;
// and send back.
cl.on('error', function(e) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment