title: Socket hang up error in Cloud Functions for Firebase
body: <p>I have a Cloud Function that is triggered by a Pub / Sub event. It uses <a href="https://github.com/request/request-promise" rel="nofollow noreferrer">request-promise</a> to make multiple GET requests from an API I use for my data.</p>
  
  <p>It works when running locally in the Cloud Functions Emulator, however in production I keep getting this error: </p>
  
  <pre><code>{ RequestError: Error: socket hang up
      at new RequestError (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:14:15)
      at Request.plumbing.callback (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:87:29)
      at Request.RP$callback [as _callback] (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:46:31)
      at self.callback (/user_code/node_modules/request/request.js:185:22)
      at emitOne (events.js:96:13)
      at Request.emit (events.js:188:7)
      at Request.onRequestError (/user_code/node_modules/request/request.js:877:8)
      at emitOne (events.js:96:13)
      at ClientRequest.emit (events.js:188:7)
      at TLSSocket.socketOnEnd (_http_client.js:346:9)
      at emitNone (events.js:91:20)
      at TLSSocket.emit (events.js:185:7)
      at endReadableNT (_stream_readable.js:974:12)
      at _combinedTickCallback (internal/process/next_tick.js:80:11)
      at process._tickDomainCallback (internal/process/next_tick.js:128:9)
    name: 'RequestError',
    message: 'Error: socket hang up',
    cause: 
     { Error: socket hang up
         at createHangUpError (_http_client.js:254:15)
         at TLSSocket.socketOnEnd (_http_client.js:346:23)
         at emitNone (events.js:91:20)
         at TLSSocket.emit (events.js:185:7)
         at endReadableNT (_stream_readable.js:974:12)
         at _combinedTickCallback (internal/process/next_tick.js:80:11)
         at process._tickDomainCallback (internal/process/next_tick.js:128:9) code: 'ECONNRESET' },
    error: 
     { Error: socket hang up
         at createHangUpError (_http_client.js:254:15)
         at TLSSocket.socketOnEnd (_http_client.js:346:23)
         at emitNone (events.js:91:20)
         at TLSSocket.emit (events.js:185:7)
         at endReadableNT (_stream_readable.js:974:12)
         at _combinedTickCallback (internal/process/next_tick.js:80:11)
         at process._tickDomainCallback (internal/process/next_tick.js:128:9) code: 'ECONNRESET' },
    options: 
     { method: 'GET',
       uri: 'https://api.coinmarketcap.com/v2/ticker/?convert=BTC&amp;start=0',
       json: true,
       callback: [Function: RP$callback],
       transform: undefined,
       simple: true,
       resolveWithFullResponse: false,
       transform2xxOnly: false },
    response: undefined }
  </code></pre>
  
  <p>Any idea why I get this socket hang up error when my Cloud Function runs?</p>
  
  <p><strong>Edit:</strong> Here is what my cloud function code looks like:</p>
  
  <pre><code>const functions = require('firebase-functions');
  // Import Admin SDK
  var admin = require("firebase-admin");
  var reqprom = require('request-promise');
  
  admin.initializeApp(functions.config().firebase);
  
  exports.minutely_tick =
    functions.pubsub.topic('minutely-tick').onPublish((event) =&gt; {
      console.log('This job runs every 5 minutes!');
      getAllData()
        .then((dataArray) =&gt; {
          const ref = admin.database().ref(`myData/`);
          ref.set(dataArray);
          return console.log('dataArray length:', dataArray.length);
        })
        .catch((error) =&gt; {
          return console.log('Caught error: ', error);
        });
    });
  
  function getAllData() {
    return new Promise((resolve, reject) =&gt; {
  
      var promises = [];
      var startInt = 0;
      for (i = 0; i &lt; 1000; i++) {
        const options = {
          method: GET,
          uri: myUrlString,
          json: true
        }
        promises.push(reqprom(options));
        startInt += 100;
      }
  
      Promise.all(promises)
        .then((dict) =&gt; {
          var array = valuesFromDict(dict);
          return resolve(array);
        })
        .catch((error) =&gt; {
          return reject(error);
        });
    });
  
  }
  
  function valuesFromDict(dict) {
    var values = Object.keys(dict).map((key) =&gt; {
      return dict[key];
    });
    return values;
  }
  </code></pre>
  
  <p>In summary, the function makes multiple API calls in a loop. And via promises I wait for all of them to complete. And then format that data into an array that I set to a Firebase Database reference.</p>