1
votes

I have implemented an XMPP client for JavaScript (using strophe.js), for which I am referring to the book Professional XMPP with JavaScript and jQuery by Jack Moffitt.

The book has a great explanation on working of XMPP JavaScript client using "strophe.js".

With my code, the XMPP web client is taking 6 seconds to connect with the XMPP server (ejabberd using BOSH), which is not desirable for my application.

Can some one explain why this is happening?

My XMPP client code is as below:

var Arthur = {

    connection: null,
    jid:"20147001@localhost",
    password: "XXXX2014",
    handle_message: function (message) {
        if ($(message).attr('from')) {

            if($(message).attr('from').match(/^abcd007@localhost/)){
            console.log("inside message received");
            var body = $(message).find('body').contents();

            var span = $("<span></span>");
            body.each(function () {
            if (document.importNode) {
            $(document.importNode(this, true)).appendTo(span);
            console.log(span);
            console.log(span.text());
            }
            });
            notiReceived(span.text());
            console.log("afte notiReceived executed");
            }
            if($(message).attr('from').match(/^xyz2014@localhost/)){
                console.log("inside message received");
                var body1 = $(message).find('body').contents();
                var span1 = $("<span></span>");
                body1.each(function () {
                if (document.importNode) {
                $(document.importNode(this, true)).appendTo(span1);
                //console.log(span.find('text'));
                console.log(span1);
                console.log(span1.text());
                }
                });
                notiReceived(span1.text());
                console.log("afte notiReceived executed");
                }

            }
        return true;
    }
};

 function sendPushNotification(to,operation,request_id,message){

     if(to=="citizen"){
     var myObject = new Object();
     myObject.FROM="Executive";
     myObject.FUNCTION=operation;
     myObject.MESSAGE = message;
     myObject.REQUESTID= request_id;

   console.log("inside citizen::"+myObject);
   var myString = JSON.stringify(myObject);
   $(this).val('');
   var msg = $msg({to: 'abcd007@localhost', type: 'chat'})
       .c('body').t(myString);
   console.log("inside keypress data is::"+msg);

   Arthur.connection.send(msg);
    }
  if(to=="technician"){

      var myObject1 = new Object();
      myObject1.FROM="Executive";
         myObject1.FUNCTION=operation;
         myObject1.MESSAGE = "Check Request Status";
         myObject1.REQUESTID= request_id;

       console.log("inside technician:"+myObject1);
       var myString1 = JSON.stringify(myObject1);
       $(this).val('');
       var msg1 = $msg({to: 'xyz2014@localhost', type: 'chat'})
           .c('body').t(myString1);
       console.log("after msg send to technician"+msg1);

       Arthur.connection.send(msg1);  
  }
 };


 function connected() {
     console.log("inside connected");

    Arthur.connection.addHandler(Arthur.handle_message,
                                 null, "message", "chat");
    console.log("inside connected");
    Arthur.connection.send($pres());
};

 function disconnected() {
    console.log("disconnected");
    Arthur.connection = null;
};

function disconnect(){
    Arthur.connection.disconnect();
};

function connectXMPP() {
    var conn = new Strophe.Connection(
            hosturl.URL+":5280/http-bind");
    console.log("inside connection");
   conn.connect(Arthur.jid, Arthur.password, function (status) {
       if (status === Strophe.Status.CONNECTED) {
           connected();
           console.log("connected");
       } else if (status === Strophe.Status.DISCONNECTED) {
          disconnected();
       }
   });

   Arthur.connection = conn;
 };
1
Are you saying that it takes 6 seconds for the conn.connect() callback to get fired? Try using your browsers network debugger to see if it's simply a slow response from ejabberd.fpsColton
yes it takes it takes 6 seconds after get fired conn.connect(),as you said how response is slow from ejabberd when it comes to bosh,i tried to connect with ejabberd server from my android app using asmak ,and on my mobile data network(2g) it is not taking time more than 1 sec!!!!!!!!!!!Dev
one observation i want to share here "conn.connect()"is making processed synchronously,so that for the time (say 6sec),browser gets hang!!!Dev

1 Answers

1
votes

I don't know whether this is the case here, but in general when connections take long to established, the cause is often a host name resolution issue. For instance, if a host has both an IPv4 and IPv6 address, but there is no IPv6 connectivity yet IPv6 is tried first, then you may get a delay until the IPv6 connection attempt has timed out.

To examine whether domain name resolution is the cause, you might want to take a look at the network traffic using a tool such as Wireshark.