Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda