Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
HTTP function times out when subscribing an FCM token to a topic in Cloud Function

Minimum reproducible code:

index.ts:

import * as admin from "firebase-admin"
import fetch, { Headers } from "node-fetch";

interface BarPayload {
  topic: string,
  token: string,
}

exports.bar = functions.https.onCall(async (data, context) => {
  if (data != null) {
    const payload: BarPayload = {
      topic: data.topic,
      token: data.token,
    }

    const url = `https://${location}-${project}.cloudfunctions.net/subscribeToTopic`
    await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        topic: payload.topic,
        token: payload.token,
      }),
    })
  }
  return null;
});

export const subscribeToTopic = functions.https.onRequest(async (req, res) => {
  const payload = req.body as BarPayload;
  fetch('https://iid.googleapis.com/iid/v1/' + payload.token + '/rel/topics/' + payload.topic, {
    method: 'POST',
    headers: new Headers({
      'Authorization': 'key=AA...Wp9',
      'Content-Type': 'application/json'
    })
  }).then(response => {
    if (response.status < 200 || response.status >= 400) {
      res.sendStatus(299)
    }
  }).catch(error => {
    console.error(error);
    res.sendStatus(299)
  })
  return Promise.resolve();
})

I'm running bar in Flutter and I see the timeout error in Logs Explorer:

textPayload: "Function execution took 60051 ms. Finished with status: timeout"


But if I change my subscribeToTopic from HTTP function to a callable function, then it works fine. For example:

exports.subscribeToTopic = functions.https.onCall(async (data, context) => {
  fetch('https://iid.googleapis.com/iid/v1/' + data.token + '/rel/topics/' + data.topic, {
    method: 'POST',
    headers: new Headers({
      'Authorization': 'key=AA...Wp9',
      'Content-Type': 'application/json'
    })
  }).then(response => {
    if (response.status < 200 || response.status >= 400) {
      console.log('Error = ' + response.error);
    }
  }).catch(error => {
    console.error(error);
  })
  return null;
});

(I know I'm making some trivial mistake, and I'm new to Typescript. Any help would be appreciated :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should not do return Promise.resolve(); in the HTTPS Cloud Function:

  1. HTTPS Cloud Functions shall be terminated with with send(), redirect() or end();
  2. return Promise.resolve(); is executed before the asynchronous call to fetch is complete.

The following should do the trick (untested):

export const subscribeToTopic = functions.https.onRequest(async (req, res) => {
    
    try {
        const payload = req.body as BarPayload;
        const response = await fetch('https://iid.googleapis.com/iid/v1/' + payload.token + '/rel/topics/' + payload.topic, {
            method: 'POST',
            headers: new Headers({
                'Authorization': 'key=AA...Wp9',
                'Content-Type': 'application/json'
            })
        });
    
        if(response.status < 200 || response.status >= 400) {
            res.status(299).send();
        }
    } catch (error) {
        res.status(400).send();
    }
    
  })

However I don't understand why you separate your business logic in two Cloud Functions. Why don't you directly fetch https://iid.googleapis.com within the bar Callable Cloud Function?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda