FCM errors using Firebase admin sdk and cloud features

Repetition of this error. Error # 1

    { Error: fcm.googleapis.com network timeout. Please try again.
    at FirebaseAppError.Error (native)
    at FirebaseAppError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
    at new FirebaseAppError (/user_code/node_modules/firebase-admin/lib/utils/error.js:70:23)
    at TLSSocket.<anonymous> (/user_code/node_modules/firebase-admin/lib/utils/api-request.js:106:51)
    at emitNone (events.js:86:13)
    at TLSSocket.emit (events.js:185:7)
    at TLSSocket.Socket._onTimeout (net.js:339:8)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5)
  errorInfo: 
   { code: 'app/network-timeout',
     message: 'fcm.googleapis.com network timeout. Please try again.' } }

Another error that I get several times. Mistake number 2

 { Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "read ECONNRESET".
    at FirebaseAppError.Error (native)
    at FirebaseAppError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
    at new FirebaseAppError (/user_code/node_modules/firebase-admin/lib/utils/error.js:70:23)
    at /user_code/node_modules/firebase-admin/lib/firebase-app.js:106:23
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)
  errorInfo: 
   { code: 'app/invalid-credential',
     message: 'Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "read ECONNRESET".' } }

Another type. Error No. 3

Error sending message: { Error: A network request error has occurred: read ECONNRESET
    at FirebaseAppError.Error (native)
    at FirebaseAppError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
    at new FirebaseAppError (/user_code/node_modules/firebase-admin/lib/utils/error.js:70:23)
    at ClientRequest.<anonymous> (/user_code/node_modules/firebase-admin/lib/utils/api-request.js:115:43)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at TLSSocket.socketErrorListener (_http_client.js:310:9)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at emitErrorNT (net.js:1276:8)
  errorInfo: 
   { code: 'app/network-error',
     message: 'A network request error has occurred: read ECONNRESET' } }

What I'm trying to do with my cloud functions is to check user data based on price comparisons, send FCM messages. The cloud function is a database trigger. Here is the code for my cloud function. I do not see any problems in the code since I use promises through.

// Checks price alerts for users
exports.priceAlertCheck = functions.database.ref('/crons/alerts/price').onWrite(event => {
  const promises = [];
  admin.database().ref(`/alerts/price`).once('value', function(alertSnapshot) {
    alertSnapshot.forEach(function(dataSnapshot) {
      promises.push(createPriceAlertPromise(dataSnapshot));
    });
  });
  return Promise.all(promises);
});

function createPriceUrl(fromCurrency, toCurrency, exchange) {
  return 'https://zzzz.com/data/price?fsym='
          +fromCurrency+'&tsyms='+toCurrency+(exchange ? '&e='+exchange : '');
}

function createPriceAlertPromise(snapshot) {
  const comboKeyArray = snapshot.key.split('-');
  const fromCurrency = comboKeyArray[0];
  const toCurrency = comboKeyArray[1];
  const exchange = comboKeyArray[2];
  return request(createPriceUrl(fromCurrency, toCurrency, exchange), function (error, response, body) {
      if (!error && response.statusCode == 200) {
        const jsonobj = JSON.parse(response.body);
        const currentPrice = jsonobj[toCurrency];
        const promises = [];

        snapshot.forEach(function(data) {
            promises.push(sendAlertNotifications(snapshot.key, data.key, currentPrice));
        });
        return Promise.all(promises);
      } else {
        console.log('Error fetching price', snapshot.key);
      }
  });
}

function sendAlertNotifications(comboKey, userId, currentPrice) {
  const getUserPromise = admin.database()
                          .ref(`/users/${userId}`)
                          .once('value');
  const getUserPriceAlertsPromise = admin.database()
                          .ref(`/user_alerts/price/${userId}`)
                          .once('value');
  return Promise.all([getUserPromise, getUserPriceAlertsPromise]).then(results => {
    const userSnapshot = results[0];
    if(!userSnapshot.val()){
      return console.log('Not user details', userId)
    }
    const instanceId = userSnapshot.val().instanceId;
    const subscriptionStatus = userSnapshot.val().subscriptionStatus;
    const priceAlertSnapshot = results[1];
    if(subscriptionStatus != 1){
      return console.log('Not Sending alerts. Subscription Expired', userId);
    }
    // Check if there are any device tokens.
    if (!priceAlertSnapshot.hasChildren()) {
      return console.log('There are no alerts to send for', comboKey, ", userId:", userId);
    }
    console.log("Alerts of users fetched for ", comboKey, " : ", priceAlertSnapshot.numChildren(), ", userId:", userId);
    const promises = [];
    priceAlertSnapshot.forEach(function(dataSnapshot) {
        promises.push(sendAlertNotification(userId, instanceId, currentPrice, dataSnapshot));
    });
    return Promise.all(promises);
  })
  .catch(error => {
    console.log("Error getting user alert details:", error, ", userId:", userId);
  });
}

function sendAlertNotification(userId, instanceId, currentPrice, dataSnapshot) {
  const comboKey = dataSnapshot.val().name;
  const comboKeyArray = comboKey.split('-');
  const fromCurrency = comboKeyArray[0];
  const toCurrency = comboKeyArray[1];
  const exchange = comboKeyArray[2];
  const alertPrice = dataSnapshot.val().value;
  if(priceAlertConditionCheck(currentPrice, dataSnapshot)) {
    // Notification details.
    const payload = {
      notification: {
        title: `${fromCurrency} Price Alert`,
        body: "You have been notified",
        sound: 'default',
        tag: comboKey
      },
      data: {
        title: `${fromCurrency} Price Alert`,
        body: "You have been notified",
        name: comboKey,
        sound: 'default',
        type: "alert"
      }
    };
    // Set the message as high priority and have it expire after 24 hours.
    var options = {
      priority: "high",
      timeToLive: 60 * 10
    };

    return admin.messaging().sendToDevice(instanceId, payload, options).then(response => {
      response.results.forEach((result, index) => {
        const error = result.error;
        if (error) {
          console.error("Failure sending message:", error, " userId:", userId, " token:", instanceId);
        }
        console.log("Successfully sent message:", response, ", userId:", userId);
      });
    })
    .catch(error => {
      console.log("Error sending message:", error, " userId:", userId, " token:", instanceId);
    });
  }
  return;
}

Currently, the data is small, and yet I get 30% failures (10 to 15 entries) in the firebase database. How will this work if there are records of 10 thousand records? How can I prevent these errors? There is also no documentation for these "app /" error codes, but only for "message /" errors.

UPDATE # 1: :

function createPriceAlertPromise(snapshot) {
  const comboKeyArray = snapshot.key.split('-');
  const fromCurrency = comboKeyArray[0];
  const toCurrency = comboKeyArray[1];
  const exchange = comboKeyArray[2];
  return rp(createPriceUrl(fromCurrency, toCurrency, exchange),
  {resolveWithFullResponse: true}).then(response => {
    if (response.statusCode === 200) {
      const jsonobj = JSON.parse(response.body);
      const currentPrice = jsonobj[toCurrency];
      const promises = [];

      snapshot.forEach(function(data) {
          promises.push(sendAlertNotifications(snapshot.key, data.key, currentPrice));
      });
      return Promise.all(promises);
    }
    throw response.body;
  }).catch(error => {
    console.log('Error fetching price', error);
  });
}

UPDATE # 2: - 540 , # 1

UPDATE # 3: : β„–1 , β„–3

// Checks price alerts for users
exports.priceAlertCheck = functions.database.ref('/crons/alerts/price').onWrite(event => {
  return admin.database().ref(`/alerts/price`).once('value').then(alertSnapshot => {
    const promises = [];
    alertSnapshot.forEach(function(dataSnapshot) {
      promises.push(createPriceAlertPromise(dataSnapshot));
    });
    return Promise.all(promises).then(response => {
      return deleteFirebaseApp();
    })
    .catch(function(error) {
      return logError(error);
    });
  });
});
function createPriceAlertPromise(snapshot) {
  const comboKeyArray = snapshot.key.split('-');
  const fromCurrency = comboKeyArray[0];
  const toCurrency = comboKeyArray[1];
  const exchange = comboKeyArray[2];
  return rp(createPriceUrl(fromCurrency, toCurrency, exchange),
  {resolveWithFullResponse: true}).then(response => {
    if (response.statusCode === 200) {
      const jsonobj = JSON.parse(response.body);
      const currentPrice = jsonobj[toCurrency];

      const forEachPromise =  new Promise(function(resolve) {
        const promises = [];
        snapshot.forEach(function(data) {
          promises.push(sendAlertNotifications(snapshot.key, data.key, currentPrice));
        });
        resolve(promises);
      });

      forEachPromise.then(promises => {
        return Promise.all(promises);
      })
      .catch(error => {
        return reportError(error, { type: 'database_query', context: 'forEach'});
      });
    } else {
      throw response.body;
    }
  }).catch(error => {
    return reportError(error, { type: 'http_request', context: 'price fetching'});
  });
}
+4
3

node request, promises. request-promise, , HTTP- , .

promises , Cloud Function . ECONNRESET , . Firebase.

+4

494:   response.results.forEach((, ) = > {

:

return RSVP.all(response.results.map((, ) = > {

"forEach" null, , . "map" , RSVP.all , promises .

, , .

0

I "solved" the problem by updating:

npm install -g firebase-tools@latest

npm install --save firebase-functions@latest firebase-admin@latest firebase@latest
0
source

Source: https://habr.com/ru/post/1681086/


All Articles