Firebase Cloud Functions Error: connect ECONNREFUSED

4.2k views Asked by At

I am trying to create a Kik Messenger bot according to their API using Firebase Cloud Functions. I am using Blaze Plan. I am trying to reply to a message that my bot received. I can receive messages on my API but when I try to reply to them I get an error. An error is not from the request callback. I see the error on Firebase Console.

Error: connect ECONNREFUSED 72.14.246.44:443

at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '72.14.246.44',
port: 443

Requests to the Kik Messenger API works on local and remote node/express app. I tried to use kik-node on Cloud Functions but it gave the same result. What I have discovered so far is that https://auth.kik.com resolves to Amazon and https://api.kik.com resolves to Google Hosting. I think they are also using Firebase Cloud Functions for their API. Can it be possible that they are blocked inbound requests? Here is the sample code of what I tried.

exports.messagepost = functions.https.onRequest((req, res) => {
  // Gives the error below
  // {
  //  Error: connect ECONNREFUSED 72.14.246.44:443
  //   at Object.exports._errnoException (util.js:1018:11)
  //   at exports._exceptionWithHostPort (util.js:1041:20)
  //   at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
  //   code: 'ECONNREFUSED',
  //   errno: 'ECONNREFUSED',
  //   syscall: 'connect',
  //   address: '72.14.246.44',
  //   port: 443
  // }
  request.post({
    uri: 'https://api.kik.com/v1/message',
    body: JSON.stringify({
      foo: 'bar'
    }),
    json: true,
    auth:{
      user:'{API_USER}',
      pass:'{API_KEY}'
    },
    headers: {
      'Content-Type'   : 'application/json'
    }
  }, (error, response) => {
    if (error) console.error(error);
    else console.log('Response: ', response.headers);
    res.status(200).end('OK');
  });
});

exports.messageget = functions.https.onRequest((req, res) => {
  // Gives the error below
  // {
  //  Error: connect ECONNREFUSED 72.14.246.44:443
  //   at Object.exports._errnoException (util.js:1018:11)
  //   at exports._exceptionWithHostPort (util.js:1041:20)
  //   at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1086:14)
  //   code: 'ECONNREFUSED',
  //   errno: 'ECONNREFUSED',
  //   syscall: 'connect',
  //   address: '72.14.246.44',
  //   port: 443
  // }
  request.get({
    uri: 'https://api.kik.com/v1/message',
    auth:{
      user:'{API_USER}',
      pass:'{API_KEY}'
    }
  }, (error, response) => {
    if (error) console.error(error);
    else console.log('Response: ', response.headers);
    res.status(200).end('OK');
  });
});

exports.verificationget = functions.https.onRequest((req, res) => {
  // Runs with no errors
  request.get({
    uri: 'https://auth.kik.com/verification/v1/check',
    qs: {
      u: 'username',
      d: 'hostname',
      debug: true
    },
    body: JSON.stringify({ data: 'debugsigneddata' }),
    headers: {
      'Content-Type'   : 'application/json' ,
      'Content-Length' : JSON.stringify({ data: 'debugsigneddata' }).length
    },
    auth:{
      user:'{API_USER}',
      pass:'{API_KEY}'
    }
  }, (error, response) => {
    if (error) console.error(error);
    else console.log('Response: ', response.headers);
    res.status(200).end('OK');
  });
});

exports.verificationpost = functions.https.onRequest((req, res) => {
  // Runs with no errors
  request.post({
    uri: 'https://auth.kik.com/verification/v1/check',
    qs: {
      u: 'username',
      d: 'hostname',
      debug: true
    },
    body: JSON.stringify({ data: 'debugsigneddata' }),
    headers: {
      'Content-Type'   : 'application/json' ,
      'Content-Length' : JSON.stringify({ data: 'debugsigneddata' }).length
    },
    auth:{
      user:'{API_USER}',
      pass:'{API_KEY}'
    }
  }, (error, response) => {
    if (error) console.error(error);
    else console.log('Response: ', response.headers);
    res.status(200).end('OK');
  });
});
2

There are 2 answers

0
William Chou On

I ran into a similar issue while implementing an OAuth2 token exchange using cloud functions instead of running a dedicated server.

This might not help the OP but to fix this error in my case, I had to add the https:// protocol to my post URL as it was missing.

If others run into this issue it might be worth checking your POST url is written correctly.

0
zetxx On
code: 'ECONNREFUSED'

means that nothing is listening on this port on remote side, you can check:

  1. router (some redirects)
  2. dns (maybe wrong resolving)

also you can try to telnet to this ip:port from the machine that are you running this app