Victor Silva
Victor Silva

Reputation: 326

Connecting to XMPP server with Javascript

So, I have been trying to develop this 2 days from now. The question is: I have a server that uses XMPP protocol to handle chats. I have to connect to it, but I'm developing a Phonegap/Cordova application, and there is not a plugin for that.

I have tried to connect to the server with Strophe.js but had no sucess. The server requires me to use plain authentication, and already provided me an encoded password.

Sample code:

$scope.onConnect = function(status){
    if (status == Strophe.Status.CONNECTING) {
        $scope.connStatus = 'Strophe is connecting.';
    } else if (status == Strophe.Status.CONNFAIL) {
        $scope.connStatus = 'Strophe failed to connect.';
    } else if (status == Strophe.Status.DISCONNECTING) {
        $scope.connStatus = 'Strophe is disconnecting.';
    } else if (status == Strophe.Status.DISCONNECTED) {
        $scope.connStatus = 'Strophe is disconnected.';
    } else if (status == Strophe.Status.CONNECTED) {
        $scope.connStatus = 'Strophe is connected.';
    }
}

$scope.chatConnect = function(ID, xmppToken){
    connection = new Strophe.Connection('chat.server.com');
    Strophe.SASLPlain.priority = 99;
    Strophe.SASLAnonymous.test = function() {
        return false;
    };
    Strophe.SASLMD5.test = function() {
        return false;
    };
    Strophe.SASLSHA1.test = function() {
        return false;
    };
    connection.rawInput = function(data){alert("Input: " + data);};
    connection.rawOutput = function(data){alert("Output: " + data);};
    connection.connect(ID, xmppToken, $scope.onConnect);
}

I am open to suggestions of new libraries too! Thanks in advance.

Upvotes: 1

Views: 1393

Answers (1)

beaver
beaver

Reputation: 17647

Here is a really simple example of an Ionic chat app deployed on Plunker (requires an XMPP server deployed as localhost): http://plnkr.co/edit/i3at7UvgqHaiL8NzN2k3

So in your controller the code could be:

$scope.usr = {username: '[email protected]', password: 'pippo', connected: false};

var server = 'test.com';  // adapt to your server config (domain)
var xmpp_server = 'http://127.0.0.1:7070/http-bind/';
var connection = null;

$scope.connect = function (usr) {
    connection = new Strophe.Connection(xmpp_server);
    connection.connect(usr.username, usr.password, onConnect);
}

Other Plunkers with example of Strophe.js usage:

http://plnkr.co/edit/EhQHDsYpDhrECmaaIlZO

http://plnkr.co/edit/F8cbsBZQUPiZ0W1v0O89

Upvotes: 3

Related Questions