title: AMQJSC0001E Connect timed out error while connecting from Paho to mosca
body: <p>Im new to IoT world ,
  I'm trying to connect server through mqtt protocol, using paho in FE.
  Backend is in node and mosca as broker.
  while i try to listen to top from angularjs app , getting these following message.</p>
  
  <pre><code>WebSocket connection to 'ws://127.0.0.1:1996/mqtt' failed: WebSocket opening handshake timed out
  </code></pre>
  
  <p>and in mqttClient.connect ->onFailure ,getting this error obj</p>
  
  <pre><code>{
    errorCode:1,
    errorMessage:"AMQJSC0001E Connect timed out.",
    invocationContext:undefined
  }
  </code></pre>
  
  <p>c:\nodejsPrjct>node server.js</p>
  
  <p>server.js content</p>
  
  <pre><code>// set up =========
  var express = require('express');
  var app = express();
  
  /*
   * log requests to the console (express4)
   * */
  var morgan = require('morgan') || '';
  
  
  /*
   * listing port for application
   * */
  var port = 1956 || process.env.PORT || 8080;
  
  
  /*
   * pull information from HTML POST
   * */
  var bodyParser = require('body-parser');
  
  
  //config  =========
  /*
   * SPA client folder
   * */
  app.use(express.static('client'));
  
  
  /*
   * comments for each request on console
   * */
  app.use(morgan('dev'));
  
  /*
   * limits json files to 50mb
   * **/
  app.use(bodyParser.json({limit: '50mb'}));
  
  // routes =========
  require('./routes/routes')(app);
  
  /*
   * MongoDb listener
   * */
  var ascoltatori = require('ascoltatori');
  
  var pubsubsettings = {
      type: 'mongo',
      url: 'mongodb://localhost:27017/mqtt',
      pubsubCollection: 'ascoltatori',
      mongo: {}
  };
  
  var settings = {
      port: 1996,
      backend: pubsubsettings
  };
  
  /*
   * Message broker
   * */
  
  var mosca = require('mosca');
  
  var server = new mosca.Server(settings, function () {
      //callback if server is slow
  });
  
  var message = {
      topic: 'cross',
      payload: parseInt(Math.random() * 100, 10), // or a Buffer
      qos: 1, // 0, 1, or 2
      retain: true // or false
  };
  
  // fired when the mqtt server is ready
  server.on('ready', serverReady);
  // fired when a message is received
  server.on('published', serverPublished);
  // fired when client connected and  ready
  server.on('clientConnected', clientConnectedSuccesful);
  
  function serverReady(data) {
      console.log('Mosca server is up and running', data);
  }
  
  function serverPublished(packet, client) {
      console.log('Published', packet.payload);
      console.log('Published', client);
  }
  
  function clientConnectedSuccesful(client) {
      console.log('client connected', client.id);
      setInterval(function () {
          server.publish(message, function () {
              message.payload = parseInt(Math.random() * 100, 10);
          });
      }, 15000);
  }
  
  // init =========
  app.listen(port);
  </code></pre>
  
  <hr>
  
  <p>from angular.js > </p>
  
  <pre><code> /*
                   * https://www.eclipse.org/paho/files/jsdoc/symbols/Paho.MQTT.Client.html
                   * */
  
                  var mqttClient = undefined;
  
                  var location = {
                      'hostname': '127.0.0.1',
                      'port': 1996
                  };
  
                  var mqttClientId = "myclientid_" + parseInt(Math.random() * 100, 10);
  
                  // Create a client instance
                  //Paho.MQTT.Client(host, port, path, clientId)
                  mqttClient = new Paho.MQTT.Client(location.hostname, location.port, mqttClientId);
  
  
                  mqttClient.onMessageArrived = function (message) {
                      console.log("onMessageArrived:" + message.payloadString);
                      $scope.currentValues = message.payloadString;
                  };
                  mqttClient.onConnectionLost = function (responseObject) {
                      if (responseObject.errorCode !== 0) {
                          console.log("onConnectionLost:" + responseObject.errorMessage);
                      }
                  };
                  var options = {
                      timeout: 5,
                      onSuccess: function (data) {
                          mqttClient.subscribe("cross", {qos: 1});
                          console.log("onConnect", data);
                      }, onFailure: function (data) {
                          console.log(data)
                      }
                  };
                  mqttClient.connect(options);
  </code></pre>
  
  <p>*downvoter please explain and downvote it.</p>