Skip to content

Instantly share code, notes, and snippets.

@metalaureate
Created April 18, 2015 22:59
Show Gist options
  • Save metalaureate/f35565b3b87c59caf507 to your computer and use it in GitHub Desktop.
Save metalaureate/f35565b3b87c59caf507 to your computer and use it in GitHub Desktop.
function mutualSubscribe(source, friend, callback) {
var XMPP = require('stanza.io');
var friend_jid = new XMPP.JID(friend.toLowerCase() + '@idelog.me/daemon');
var source_jid = new XMPP.JID(source.toLowerCase() + '@idelog.me/daemon');
var source_client = XMPP.createClient({
jid: source_jid,
password: source.split("").reverse().join(""),
transport: 'bosh',
boshURL: config.get('xmpp.bosh_url')
});
var friend_client = XMPP.createClient({
jid: friend_jid,
password: friend.split("").reverse().join(""),
transport: 'bosh',
boshURL: config.get('xmpp.bosh_url')
});
var source_session_timeout = setTimeout(function () {
console.log('xmpp', 'no events, hang');
source_client.disconnect();
}, 15000);
var friend_session_timeout = setTimeout(function () {
console.log('xmpp', 'no events, hang');
friend_client.disconnect();
}, 15000);
source_client.on('sasl:failure', function (error) {
var msg=source_jid.local + ' '+error.condition;
throw msg;
});
friend_client.on('sasl:failure', function (error) {
var msg=friend_jid.local + ' '+error.condition;
throw msg;
});
source_client.on('session:started', function () { // STEP 1
console.log('xmpp session started for ' + source_jid.local);
source_client.getRoster(function (error, roster) {
var contact = _.find(roster.roster.items, function (r) {
return r.jid.local == friend_jid.local; // pres.from.local
});
var sendSubscribe = true;
if (contact) {
sendSubscribe = contact.subscription !== 'both' && !contact.subscriptionRequested;
}
console.log('source subscription request', sendSubscribe);
source_client.on('subscribe', function (pres) {
source_client.acceptSubscription(pres.from); // accept the subscription we received
if (sendSubscribe) {
source_client.subscribe(pres.from); // send a subscribe the other way
}
});
source_client.sendPresence();
source_client.subscribe(friend_jid);
friend_client.connect(); //-- friend logs in after source has sent invite
});
});
friend_client.on('session:started', function () { // STEP 2
console.log('xmpp session started for ' + friend_jid.local);
friend_client.getRoster(function (error, roster) {
var contact = _.find(roster.roster.items, function (r) {
return r.jid.local == source_jid.local; // pres.from.local
});
var sendSubscribe = true;
if (contact) {
sendSubscribe = contact.subscription !== 'both' && !contact.subscriptionRequested;
}
console.log('friend subscription request', sendSubscribe);
friend_client.on('subscribe', function (pres) {
friend_client.acceptSubscription(pres.from);
if (sendSubscribe) {
friend_client.subscribe(pres.from);
}
});
friend_client.sendPresence();
friend_client.subscribe(source_jid);
});
});
source_client.on('subscribed', function (result) { // STEP 4
console.log('xmpp', source_jid.local + ' is now subscribed to ' + friend_jid.local);
clearTimeout(source_session_timeout);
source_client.disconnect();
});
friend_client.on('subscribed', function (result) { // STEP 4
console.log('xmpp', friend_jid.local + ' is now subscribed to ' + source_jid.local);
clearTimeout(friend_session_timeout);
friend_client.disconnect();
});
friend_client.on('session:end', function () { // STEP 5
console.log(friend_jid.local, 'xmpp session ended');
// -- check if we really succeeded
callback(null, null);
});
source_client.on('session:end', function () { // STEP 5
console.log(source_jid.local, 'xmpp session ended');
});
source_client.connect();
}
@metalaureate
Copy link
Author

Output

xmpp session started for metalaureate
source subscription request true
xmpp session started for nambikawa
friend subscription request true
xmpp metalaureate is now subscribed to nambikawa
metalaureate xmpp session ended
xmpp nambikawa is now subscribed to metalaureate
nambikawa xmpp session ended

But prosody shows:

idelog.me | metalaureate | roster   | [email protected]       | json   | {"groups":{},"subscription":"to"}

 idelog.me | metalaureate | roster   | [email protected]             | json   | {"groups":{},"subscription":"to"}

 idelog.me | metalaureate | roster   | [email protected]       | json   | {"groups":{},"subscription":"to"}

 idelog.me | metalaureate | roster   | [email protected] | json   | {"groups":{},"subscription":"to"}

 idelog.me | metalaureate | roster   |                           | json   | {"__hash":[false,{"version":27,"pending":{}}]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment