title: mqtt mosca pahojs WebSocket connection to 'ws://localhost:1884/mqtt' failed/timeout
body: <h2>Broker</h2>
  
  <pre><code>var mosca = require('mosca')
  
  
  var settings = {
    port: 1884
  };
  
  //here we start mosca
  var server = new mosca.Server(settings);
  server.on('ready', setup);
  
  // fired when the mqtt server is ready
  function setup() {
    console.log('Mosca server is up and running')
  }
  
  // fired whena  client is connected
  server.on('clientConnected', function(client) {
    console.log('client connected', client.id);
  });
  
  // fired when a message is received
  server.on('published', function(packet, client) {
    if (packet.cmd === 'publish') {
      //Qui uso mongo DB 
      console.log('Published: ', packet.payload.toString('utf8'));
    }
  });
  
  // fired when a client subscribes to a topic
  server.on('subscribed', function(topic, client) {
    console.log('subscribed : ', topic);
  });
  
  // fired when a client subscribes to a topic
  server.on('unsubscribed', function(topic, client) {
    console.log('unsubscribed : ', topic);
  });
  
  // fired when a client is disconnecting
  server.on('clientDisconnecting', function(client) {
    console.log('clientDisconnecting : ', client.id);
  });
  
  // fired when a client is disconnected
  server.on('clientDisconnected', function(client) {
    console.log('clientDisconnected : ', client.id);
  });
  </code></pre>
  
  <h2>client.html</h2>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
          &lt;meta charset="utf-8" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;script src="./mqttws3.1.js"&gt;&lt;/script&gt;
        &lt;script&gt;
    var client = new Paho.MQTT.Client( 'localhost', 1884, 'clientId');
  
    client.onConnectionLost = onConnectionLost;
    client.onMessageArrived = onMessageArrived;
    client.connect({onSuccess:onConnect});
  
    function onConnect() {
    // Once a connection has been made, make a subscription and send a message.
    console.log("onConnect");
    client.subscribe("/World");
    message = new Paho.MQTT.Message("Hello");
    message.destinationName = "/World";
    client.send(message); 
  };
  function onConnectionLost(responseObject) {
    if (responseObject.errorCode !== 0)
    console.log("onConnectionLost:"+responseObject.errorMessage);
  };
  function onMessageArrived(message) {
    console.log("onMessageArrived:"+message.payloadString);
    client.disconnect(); 
  };  
        &lt;/script&gt; 
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I run the broker</p>
  
  <pre><code>node broker
  </code></pre>
  
  <p>than I call the client.html by a web server
  like </p>
  
  <pre><code>http://localhost/client.html
  </code></pre>
  
  <p>I get after a while</p>
  
  <blockquote>
    <p>Firefox can't establish a connection to the server at
    ws://localhost:1884/mqtt. this.socket = new WebSocket(wsurl,
    ["mqtt"]);</p>
    
    <p>Chrome : WebSocket connection to 'ws://localhost:1884/mqtt' failed:
    WebSocket opening handshake timed out</p>
  </blockquote>
  
  <p>I don't know which way to turn :(</p>
  
  <p>Can you help me please ?</p>