title: Dialogflow connect to MQTT broker using Firebase?
body: <p>I'm trying to use the Cloud Functions for Firebase to make a Dialogflow intent result in an MQTT message being published. I've managed to get the data I needed and all I still am not able to accomplish is:</p>
  
  <ol>
  <li>Establishing a connection to the MQTT broker;  </li>
  <li>Publishing to said broker.</li>
  </ol>
  
  <p>As the second one requires the former, it is not my concern <em>yet</em>.  </p>
  
  <p>The way I see it is: either my code is wrong or MQTT is included in what Firebase calls "external network". And I'm here, mainly, to make sure I don't upgrade to a paid plan for nothing.</p>
  
  <p><code>index.js</code>:</p>
  
  <pre class="lang-js prettyprint-override"><code>'use strict';
  
  const functions = require('firebase-functions');
  const { WebhookClient, Card, Suggestion } = require('dialogflow-fulfillment');
  
  var mqtt = require('mqtt');
  
  process.env.DEBUG = 'dialogflow:debug';
  const HOST = 'broker.mqttdashboard.com';
  const PORT = 1883;
  const TOPIC = 'topic/voice_recog';
  
  exports.dialogflowFirebaseFulfillment = functions.region('europe-west1').https.onRequest((request, response) =&gt; {
  
    const agent = new WebhookClient({ request, response });
  
    try {
  
      function publishParameter(agent) {
  
        let message = agent.parameters.param_test;
  
        return publishToMqtt(message).then((output) =&gt; {
          agent.add(output);
        }).catch(error =&gt; {
          agent.add('error from publishToMqtt');
        });
      }
  
      let intentMap = new Map();
      intentMap.set('Repeat parameter', publishParameter);
      agent.handleRequest(intentMap);
  
    }
  
    catch (err) {
      console.error(err);
      agent.add(err.message);
      agent.send_();
    }
  });
  
  function publishToMqtt(message) {
  
     console.log("------------------------------");
     console.log("Topic: \""+TOPIC+"\"");
     console.log("Message: \""+message+"\"");
     console.log("------------------------------");
  
     return new Promise((resolve, reject) =&gt; {
  
        var options = {
           port: PORT,
           host: HOST,
           clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
           keepalive: 60,
           reconnectPeriod: 1000,
           protocolId: 'MQIsdp',
           protocolVersion: 3,
           clean: true,
           encoding: 'utf8'
        };
  
        var client = mqtt.connect("mqtt://"+HOST, {port: PORT});
  
        // is not executed
        client.on('connect', function () { 
           console.log('client connected');
        });
  
        // is not executed
        client.publish(TOPIC, message, {}, function (err) { 
           console.log("Tried publishing \""+message+"\" to \""+TOPIC+"\".");
           if (err) {
              console.log("But the future refused to change:" + err);
              reject();
           }
           else {
              resolve(message);
              client.end();
              clearTimeout(noResp);
           }
        });
  
        let noResp = setTimeout(() =&gt; {
           console.log("No connection"); // always gives this result
           reject();
           client.end();
        }, 5000);
  
     });
  }
  </code></pre>
  
  <p><code>package.json</code>:</p>
  
  <pre><code>{
    "name": "assistant-to-mqtt",
    "description": "publishes intent parameter to MQTT broker",
    "engines": {
      "node": "8"
    },
    "version": "0.0.1",
    "private": true,
    "scripts": {
      "start": "firebase serve --only functions:publish_mqtt",
      "deploy": "firebase deploy --only functions:publish_mqtt"
    },
    "dependencies": {
      "actions-on-google": "^2.1.3",
      "dialogflow-fulfillment": "^0.4.1",
      "firebase-admin": "^5.12.1",
      "firebase-functions": "^2.2.1",
      "mqtt": "^2.13.0"
    }
  }
  </code></pre>
  
  <p>All help is appreciated. Thank you.</p>
  
  <p>EDIT: The log.</p>
  
  <p><a href="https://i.stack.imgur.com/KDGr4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KDGr4.png" alt="log"></a></p>