wkh
wkh

Reputation: 35

Firebase Cloud functions: ECONNREFUSED

I am trying the Optimizing Networking of firebase cloud functions like here with Typescript

const http = require('http');
const functions = require('firebase-functions');
const agent = new http.Agent({keepAlive: true});

export const getXXX = functions.https.onRequest((request, response) => {
    const req = http.request({
        host: 'localhost',
        port: 443,
        path: '',
        method: 'GET',
        agent: agent,
    }, res => {
        let rawData = '';
        res.setEncoding('utf8');
        res.on('data', chunk => { rawData += chunk; });
        res.on('end', () => {
            response.status(200).send(`Data: ${rawData}`);
        });
    });
    req.on('error', e => {
        response.status(500).send(`Error: ${e.message}`);
    });
    req.end();
});

but I keep getting

error: connect ECONNREFUSED 127.0.0.1:443

I am not very familiar with TypeScript and js so please help me.
Another question when is res.on 'Data' gets triggered ?

Upvotes: 2

Views: 3858

Answers (4)

Jeremy
Jeremy

Reputation: 703

If you are calling a cloud function from another cloud function using the emulator, make sure you are using 127.0.0.1: instead of localhost: in your http request.

Upvotes: 0

imbatman
imbatman

Reputation: 518

You can run Cloud Functions on localhost. All you need to do is run a local emulator of the Cloud services. Which Google has provided! It's a really awesome tool and a great setup!

Follow these steps for the Firebase tool suite: https://firebase.google.com/docs/functions/local-emulator

Follow these steps for the Cloud tool suite: https://cloud.google.com/functions/docs/emulator

They are pretty much similar.

You do not need Blaze plan, you can use the "pay as you go" plan, which includes the free tier quota. "Free usage from Spark plan included*" https://firebase.google.com/pricing

Upvotes: 0

wkh
wkh

Reputation: 35

Turns out I need to be on a paid plan in order to make external HTTP requests from inside my function.

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317542

You can't access anything on "localhost" (127.0.0.1) in Cloud Functions. I suspect that you meant to put a different host in there, and ensure that your project is on the Blaze plan to enable outgoing connections to services not fully controlled by Google.

Upvotes: 0

Related Questions