title: Unable to connect to Mosquitto over Websocket
body: <p>I am unable to connect to my local Mosquitto 1.4.10 broker from a JavaScript client over a Websocket.</p>
  
  <p>The same JavaScript client is successfully connecting to the public broker at test.mosquitto.org on port 8080 over a Websocket.</p>
  
  <p>The MQTT protocol connection on port 1883 is working fine, which I tested using mosquitto_pub and mosquitto_sub.</p>
  
  <p>My broker is set up within a VirtualBox running Ubuntu 14.04. </p>
  
  <p>I have libwebsockets installed on the same virtual machine.</p>
  
  <p>My local broker was compiled with <strong>WITH_WEBSOCKETS:=yes</strong> in the <em>config.mk</em> file</p>
  
  <p>I am loading the JavaScript client web page from the same virtual machine from a Firefox browser and seeing the following error message in the browser console:</p>
  
  <blockquote>
    <p>Firefox can't establish a connection to the server at
    ws://localhost:8080/mqtt</p>
  </blockquote>
  
  <p>Your suggestions on fixing this will be greatly appreciated.</p>
  
  <p>Thanks.</p>
  
  <p>Here is my Mosquitto .conf file:</p>
  
  <pre><code>port 1883
  listener 8080
  protocol websockets
  
  log_type all
  websockets_log_level 1023
  connection_messages true
  </code></pre>
  
  <p>Here is the Mosquitto server's log (with websockets logging level set to 1023, and verbose logging turned on - no messages appear when I load the JavaScript web page):</p>
  
  <blockquote>
    <p>1481381105: mosquitto version 1.4.10 (build date 2016-12-10
    18:47:37+0530) starting<br>
    1481381105: Config loaded from /etc/mosquitto/mosquitto.conf.<br>
    1481381105: Opening websockets listen socket on port 8080.<br>
    1481381105: Initial logging level 1023</p>
    
    <p>1481381105: Libwebsockets version: 2.1.0 manavkumarm@manav-alljoyn</p>
    
    <p>1481381105: IPV6 not compiled in<br>
    1481381105: libev support not compiled in<br>
    1481381105: libuv support not compiled in<br>
    1481381105: Threads: 1 each 1024 fds<br>
    1481381105:  mem: platform fd map:  4096 bytes<br>
    1481381105:  Compiled with OpenSSL support<br>
    1481381105: Creating Vhost 'default' port 8080, 3 protocols, IPv6 off<br>
    1481381105: Using non-SSL mode<br>
    1481381105:  Listening on port 8080<br>
    1481381105: mem: per-conn:          376 bytes + protocol rx buf<br>
    1481381105: canonical_hostname = mqtt<br>
    1481381105: Opening ipv4 listen socket on port 1883.<br>
    1481381105: Opening ipv6 listen socket on port 1883.</p>
  </blockquote>
  
  <p>Here is the JavaScript source code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;html&gt;
  
   &lt;body&gt;
    &lt;script src="mqttws31.js"&gt;&lt;/script&gt;
    &lt;script&gt;
     try
     {
           // Create a client instance
      console.log("Creating client object...");
      client = new Paho.MQTT.Client("localhost", Number(8080), "manav");
      //client = new Paho.MQTT.Client("test.mosquitto.org", Number(8080), "manav");
       
      // set callback handlers
      console.log("Setting handlers...");
      client.onConnectionLost = onConnectionLost;
      client.onMessageArrived = onMessageArrived;
       
      // connect the client
      console.log("Connecting...");
      client.connect( {
                                    onSuccess: onConnect, 
                                    mqttVersion: 4
                                  });
     }
     catch (e)
     {
      console.log("Error: " + e.description);
     }
      
     // called when the client connects
     function onConnect() 
     {
       // Once a connection has been made, make a subscription and send a message.
       console.log("Connected");
       setTimeout( function() {
        client.subscribe("world");
        message = new Paho.MQTT.Message("Hello");
        message.destinationName = "world";
        client.send(message);
        //client.disconnect();     
       }, 5000);
     }
      
     // called when the client loses its connection
     function onConnectionLost(responseObject) {
       if (responseObject.errorCode !== 0) {
         console.log("Connection lost: " + responseObject.errorMessage);
       }
     }
      
     // called when a message arrives
     function onMessageArrived(message) {
       console.log("Received Message: " + message.payloadString);
       client.disconnect();
     }
  
     &lt;/script&gt;
  
    &lt;h1&gt;My MQTT Websockets Example&lt;/h1&gt; 
  
   &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>