This is client client side of xmpp connection. I am not sure whether it will make any sense to you. Here i have setup a callback for the event, if there is any new message, on event method will be invoked.
// connection and service value are as follows
CJ.app.conn: null,
CJ.app.service: ‘pubsub.mammoth.local’,
CJ.app. node: ‘CJNode’,
CJ.app.NS_DATA_FORMS: “jabber:x:data”,
CJ.app.NS_PUBSUB: “http://jabber.org/protocol/pubsub”,
CJ.app.NS_PUBSUB_OWNER: “http://jabber.org/protocol/pubsub#owner”,
CJ.app.NS_PUBSUB_ERRORS: “http://jabber.org/protocol/pubsub#errors”,
CJ.app.NS_PUBSUB_NODE_CONFIG: “http://jabber.org/protocol/pubsub#node_config”,
Ext.define(‘CJ.view.MainList’, {
extend: ‘Ext.dataview.DataView’,
xtype : ‘mainlist’,
config: {
cls: ‘categories-list’,
flex: 2
},
initialize: function() {
if (CJ != null) {
this.connected();
}
},
connected: function() {
// send negative presence send we’re not a chat client
// CJ.connection.send(‘priority -1’);
CJ.app.conn.send($pres().c(“priority”).t("-1"));
if (CJ.app.node.length > 0) {
// first, set up a callback for the events
CJ.app.conn.addHandler( this.on_event,
null, “message”, null, null, CJ.app.service);
// now subscribe
var subiq = $iq({to: CJ.app.service, type: “set”})
.c(‘pubsub’, {xmlns: CJ.app.NS_PUBSUB})
.c(‘subscribe’, {node: CJ.app.node, jid: CJ.app.conn.jid});
CJ.app.conn.sendIQ(subiq, this.subscribed, this.subscribe_error);
CJ.app.conn.flush();
}
},
configured: function (iq) {
//$(document).trigger(“broadcast_started”);
},
configure_error: function (iq) {
CJ.app.show_error("CJ configuration failed with " +
CJ.app.make_error_from_iq(iq));
},
on_event: function(msg) {
if ($(msg).find(‘x’).length > 0) {
var message = $(msg).find(‘field[var=“message”] value’).text();
alert(message);
} else if ($(msg).find(‘delete[node="’ + CJ.app.node + ‘"]’)
.length > 0) {
}
return true;
},
// subscription callbacks
subscribed: function(iq) {
//alert(‘subscribed successfully’);
},
subscribe_error: function(iq) {
this.show_error('Subscription falied with '+ this.make_error_from_iq(iq));
},
show_error: function (msg) {
CJ.app.conn.disconnect();
CJ.app.conn = null;
alert(msg);
},
// error handling helpers
make_error_from_iq: function (iq) {
var error = $(iq)
.find(’*[xmlns="’ + Strophe.NS.STANZAS + ‘"]’)
.get(0).tagName;
var pubsub_error = $(iq)
.find(’*[xmlns="’ + CJ.app.NS_PUBSUB_ERRORS + ‘"]’);
if (pubsub_error.length > 0) {
error = error + “/” + pubsub_error.get(0).tagName;
}
return error;
},
showOverlay : function() {
}
});
This code will work fine if I make fresh login on newly created node. Surprisingly it works on existing node as well but sometimes stopped working suddenly. Does it mean the xmpp loses connection? it seems to be not reliable.