Skip to content

Instantly share code, notes, and snippets.

View lfcipriani's full-sized avatar
💪
www.beat81.com

Luis Cipriani lfcipriani

💪
www.beat81.com
View GitHub Profile
function onIq(xml) {
// in this case, the IQ stanzas handled are just subscription results
xmlDom = $(xml);
var iqId = xmlDom.attr('id');
if (iqId == terms[0] || iqId == terms[1]) {
if (xmlDom.find('subscription:first').attr('subscription') == 'subscribed') {
console.log("Subscribed to "+iqId);
}
}
return true;
var Collecta = {
subscribeSearchStanza: function(searchName) {
// generate XML for a search subscription on Collecta XMPP API
return $iq({type: 'set', from: anonymous_jid, to: 'search.collecta.com', id: searchName })
.c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
.c('subscribe', {node: 'search', jid: anonymous_jid})
.up().c('options')
.c('x', {xmlns: 'jabber:x:data', type: 'submit'})
.c('field', {"var": 'FORM_TYPE', type: 'hidden'})
.c('value').t('http://jabber.org/protocol/pubsub#subscribe_options')
function startComparison() {
// creates 2 search subscriptions and send the request to Collecta XMPP API via Strophejs
console.log("Subscribing to nodes: "+ terms[0] +" and "+ terms[1]);
connection.send(Collecta.subscribeSearchStanza(terms[0]).tree());
connection.send(Collecta.subscribeSearchStanza(terms[1]).tree());
}
function onPresence(prs) {
// in this case, presence got from the XMPP server means to activate UI and allow user to enter the 2 terms to compare
console.log("Got presence!");
anonymous_jid = $(prs).attr('to');
// ... outras coisas
return true;
}
@lfcipriani
lfcipriani / real-time-comparison.js
Created April 19, 2012 03:03
real-time-comparison.js 2
// real-time-comparison.js
function onConnect(status)
{
if (status == Strophe.Status.CONNECTING) {
// ... outros status
} else if (status == Strophe.Status.CONNECTED) {
// adding one handler for each type of XMPP stanza
connection.addHandler(onPresence, null, 'presence', null, null, null);
connection.addHandler(onIq, null, 'iq', null, null, null);
connection.addHandler(onMessage, null, 'message', null, null, null);
@lfcipriani
lfcipriani / real-time-comparison.js
Created April 19, 2012 03:02
real-time-comparison.js
// initiating a BOSH connection to create an anonymous connection to the Collecta XMPP server
connection = new Strophe.Connection(Config.BOSH_SERVICE);
connection.connect(Config.HOST, null, onConnect);
@lfcipriani
lfcipriani / config.js
Created April 19, 2012 03:01
config.js
// config.js
var Config = {
API_KEY: 'YOUR_COLLECTA_API_KEY',
BOSH_SERVICE: 'http://collecta.com/xmpp-httpbind',
HOST: "guest.collecta.com"
};
@lfcipriani
lfcipriani / gist:2417815
Created April 19, 2012 01:49
iq xmpp stanza
<iq type="set" id="an_id"
from="cipriani@talleye.com/casa"
to="talleye.com">
<query xmlns="jabber:iq:roster"/>
</iq>
@lfcipriani
lfcipriani / gist:2417804
Created April 19, 2012 01:48
message xmpp stanza
<message to="pedro@shorteye.com/escitorio"
from="cipriani@talleye.com/casa"
type="chat" >
<body>Cadê você?</body>
</message>
@lfcipriani
lfcipriani / gist:2417802
Created April 19, 2012 01:47
xmpp presence stanza
<presence from="cipriani@talleye.com/casa">
<status>Ouvindo música...</status>
</presence>