0

I have a pubsub cloudfunction that runs every minute, to send a pushnotification to users

export const sendScheduledNotifications = functions
  .region(REGION)
  .runWith({
    timeoutSeconds: 540,
  })
  .pubsub.schedule("* * * * *")
  .timeZone("Europe/Amsterdam")
  .onRun(async (_) => {
    const scheduledNotifications = await getNotificationsAfterDate(Date.now());

    const promises = [];

    if (scheduledNotifications.length > 0) {
      const userIds = scheduledNotifications.map((notification) => notification.userId);
      const users: AppUser[] = userIds.length == 0 ? [] : await userRepo.listUsers("id", "in", userIds);

      for (const notification of scheduledNotifications) {
        const user = users.find((user) => user.id === notification.userId)!;
        promises.push(sendScheduledNotification(notification, user));
      }
    }

    await Promise.all(promises);
  });

But I keep getting the following error in the firebase console

sendScheduledNotifications
Error: Error while making request: connect ECONNREFUSED XXX.XXX.XX.XX:XXX. Error code: ECONNREFUSED
    at FirebaseAppError.FirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:44:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (/workspace/node_modules/firebase-admin/lib/utils/error.js:90:28)
    at new FirebaseAppError (/workspace/node_modules/firebase-admin/lib/utils/error.js:125:28)
    at /workspace/node_modules/firebase-admin/lib/utils/api-request.js:211:19
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async sendScheduledNotification (/workspace/lib/index.js:1024:5)
    at async Promise.all (index 0)
    at async /workspace/lib/index.js:973:5 

Al sendScheduledNotification does is this

 sendMulticast(tokens: string[], notificationData: PushNotificationData): Promise<admin.messaging.BatchResponse> {
    const data: admin.messaging.MulticastMessage = {
      data: { data: JSON.stringify(notificationData) },
      tokens: tokens,
      android: {
        priority: "high",
      },
      apns: {
        payload: {
          aps: {
            contentAvailable: true,
          },
        },
        headers: {
          "apns-push-type": "background",
          "apns-priority": "5",
          "apns-topic": "io.flutter.plugins.firebase.messaging",
        },
      },
    };

    return this.messagingInstance.sendMulticast(data);
  }

What could be the cause of this?

anonymous-dev
  • 2,897
  • 9
  • 48
  • 112
  • Does this answer your question? [Firebase Cloud functions: ECONNREFUSED](https://stackoverflow.com/questions/50955122/firebase-cloud-functions-econnrefused) – Martin Zeitler Oct 20 '21 at 22:59

1 Answers1

0

ECONNREFUSED is an error raised by Node when the target machine or service is not currently active:

ECONNREFUSED (Connection refused): No connection could be made because the target machine actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host.

Since your functions connect to the iOS/Android notification services, have you confirmed if you are connecting to these services correctly? Additionally, you can verify more possible causes. If you are using the function emulator for local testing, this error can appear if the emulator is not running. At the time of this answer, there also is an issue in the Firebase Cloud Messaging status.

enter image description here

You can check if this error stops showing up when the status changes back. Finally, according to this related thread, functions created with Firebase’s free Spark plan do not support outbound connections and can show this specific error.

ErnestoC
  • 2,660
  • 1
  • 6
  • 19