Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMPP: AngularJs + Strophe.js

I have a basic XMPP client working on strophe.js.

On login I create handlers such as

connect = new Strophe.Connection('http://localhost/http-bind');
...
...

    connect.addHandler(on_message, null, "message", "chat");
    connect.addHandler(on_presence, null, "presence");

...
...

and then I "listen" to those

function on_presence(presence) {
// handling presence
}

function on_message(presence) {
// handling presence
}

So I am trying to "convert" it into AngularJS. The first part is pretty straight forward. I have a controller which handles the login part just fine:

angular.module('app').controller('loginCtrl', function($scope) {
connect = new Strophe.Connection('http://website.com/http-bind');

connect.connect(data.jid, data.password, function (status) {
  if (status === Strophe.Status.CONNECTED) {
    connect.addHandler(on_message, null, "message", "chat");
    connect.addHandler(on_presence, null, "presence");
  }
}
})

But how do I actually begin listening to those events (on_message, on_presence) in the context of angular across all the controllers I have.

like image 590
luiquao Avatar asked Dec 11 '22 05:12

luiquao


2 Answers

Wrap Strophe in an Angular Service. Angular Services are meant to be use as singletons, so you will be able to instantiate the Strophe Service once, and use it everywhere (using Dependency Injection).

like image 146
Gilad Beeri Avatar answered Dec 23 '22 20:12

Gilad Beeri


As suggested above (or bellow) I wrapped strophe in a service so my login "mechanism" looks like this:

.controller('loginCtrl', function(xmppAuth) {

    xmppAuth.auth(login, password);

})

any my service:

.service('xmppAuth', function() {

return {

auth: function(login, password) {
   connect = new Strophe.Connection('http://mydomain.net/http-bind');
   connect.connect(login, password, function (status) {
       if (status === Strophe.Status.CONNECTED) {
           // we are in, addHandlers and stuff
       }
   }
}

}

})
like image 21
luiquao Avatar answered Dec 23 '22 19:12

luiquao