Last active
August 29, 2015 14:19
-
-
Save metalaureate/b2011cc375764b28d5fe to your computer and use it in GitHub Desktop.
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
function mutualSubscribe(source, friend, callback) { | |
var XMPP = require('stanza.io'); | |
var friend_jid = friend.toLowerCase() + '@xxxx.me'; | |
var source_jid = source.toLowerCase() + '@xxxx.me'; | |
var source_client = XMPP.createClient({ | |
jid: source_jid, | |
password: 'xyz', | |
transport: 'bosh', | |
boshURL: config.get('xmpp.bosh_url') | |
}); | |
var friend_client = XMPP.createClient({ | |
jid: friend_jid, | |
password: 'xyz', | |
transport: 'bosh', | |
boshURL: config.get('xmpp.bosh_url') | |
}); | |
function notAlreadySubscribed(jid, client, callback) { | |
var sendSubscribe = true; | |
client.getRoster(function (error, roster) { | |
if (error) throw error; | |
var contact = (roster.roster) ? _.find(roster.roster.items, function (r) { | |
return r.jid.bare == jid; // pres.from.local | |
}) : null; | |
console.log('roster','looking up '+jid +' for '+client.jid.bare,contact); | |
if (contact) { | |
sendSubscribe = (contact.subscription !== 'both' || (contact.subscription !== 'to')) && !contact.subscriptionRequested; //|| contact.subscription !== 'from' | |
} | |
}); | |
callback(null, sendSubscribe); | |
} | |
source_client.on('subscribe', function (pres) { | |
source_client.acceptSubscription(pres.from.bare); // accept the subscription we received | |
console.log('4. ' + source + ' accepts subscription from ' + pres.from.bare); | |
}); | |
friend_client.on('subscribe', function (pres) { | |
friend_client.acceptSubscription(pres.from.bare); // accept the subscription we received | |
console.log('2. ' + friend + ' accepts subscription from ' + pres.from.bare); | |
notAlreadySubscribed(pres.from.bare, friend_client, function (err, sendSubscribe) { | |
if (sendSubscribe) { | |
console.log('3. ' + friend + ' sends subscribe to ' + pres.from.bare); | |
friend_client.subscribe(pres.from.bare); // send a subscribe the other way | |
} | |
}); | |
}); | |
source_client.on('session:started', function () { | |
console.log('xmpp session started for ' + source_jid); | |
source_client.sendPresence(); | |
console.log('1.' + source + ' sends subscribe to ' + friend_jid); | |
source_client.subscribe(friend_jid); | |
}); | |
friend_client.on('session:started', function () { | |
console.log('xmpp session started for ' + friend_jid); | |
friend_client.sendPresence(); | |
}); | |
source_client.on('subscribed', function (pres) { | |
console.log('!!!', '6. ' + source + ' is now subscribed to ' + pres.from.bare); | |
}); | |
friend_client.on('subscribed', function (pres) { | |
console.log('!!!', '5. ' + friend + ' is now subscribed to ' + pres.from.bare); | |
// source_client.sendPresence(); | |
// source_client.disconnect(); | |
// friend_client.disconnect(); | |
// clearTimeout(friend_session_timeout); | |
}); | |
friend_client.on('session:end', function () { | |
console.log(friend_jid, 'xmpp session ended'); | |
// -- check if we really succeeded | |
callback(null, null); | |
}); | |
source_client.on('session:end', function () { | |
console.log(source_jid, 'xmpp session ended'); | |
//friend_client.disconnect(); | |
}); | |
source_client.connect(); | |
friend_client.connect(); | |
} | |
mutualSubscribe('userA', 'userB', function (error, result) { | |
console.log('subscription processed'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment