Skip to content

Instantly share code, notes, and snippets.

@johnf
Created October 30, 2013 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnf/7228007 to your computer and use it in GitHub Desktop.
Save johnf/7228007 to your computer and use it in GitHub Desktop.
Simple jabber server
// Based on code at https://github.com/astro/node-xmpp/blob/master/examples/c2s.js
var xmpp = require('node-xmpp');
var c2s = new xmpp.C2SServer({
port: 5222,
domain: 'localhost'
});
var sessions = {};
c2s.on('connect', function(client) {
c2s.on('register', function(opts, cb) {
cb(true);
});
client.on('authenticate', function(opts, cb) {
// Auth everyone
cb(null);
});
client.on('online', function() {
sessions[client.jid.bare().toString()] = client;
sessions[client.jid.toString()] = client;
});
client.on('stanza', function(stanza) {
if (stanza.is('message')) {
// This might need to be different for Flapjack as it cares about conferences
// Will need to track members and then fan out the message to them
if (stanza.attrs.to.match(/conference/)) { return; }
sessions[stanza.attrs.to].send(stanza);
}
else if (stanza.is('presence')) {
if (stanza.attrs.type == 'subscribe' || stanza.attrs.type == 'subscribed') {
sessions[stanza.attrs.to].send(stanza);
}
}
});
client.on('disconnect', function() {
delete sessions[client.jid.bare().toString()];
delete sessions[client.jid.toString()];
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment