delpo
delpo

Reputation: 239

Firebase cloud pubsub subscriptions stops listening for messages

I'm trying to connect my firebase cloud functions project to a third party pub/sub (a separate project). According to this thread this is not possible, so there's no traditional way to make this work. However, I've tried to manually subscribe to certain topics using the @google-cloud/pubsub client on my firebase cloud functions. I need to react to pub/sub messages and write/update certain documents.

Example (minimal):

I have a pubsub subscription on sub.ts:

const pubSubClient = new PubSub({
  projectId: config.project_id,
  credentials: {
    client_email: config.client_email,
    private_key: config.private_key
  }
});

I subscribe to a certain topic to do some business logic

const subscription = pubSubClient.subscription('my-subscription');

this.subscription.on('message', async (message) => {
  try {
    message.ack();
    const event = parseData(message.data);
    await admin.firestore().collection('my-collection').add(event);
  } catch (e) {
    console.error(e);
  }
});

Then this file is imported within the index.js where I declare most CF functions.

import * as admin from 'firebase-admin';
admin.initializeApp();
import './sub';
export { myFunction } from './modules/my-module';
export { myOtherFunction } from './modules/other-module';

It appears that my subscriptions die out after a time and messages won't go through. If I redeploy my functions it appears to be working for a time, but then it stops listening to messages. I've read that firebase cloud functions are stateless, so in this case I need a "stateful" module within my firebase project. Is this possible? Or should I manage this on another server?.

Thanks!

Upvotes: 0

Views: 562

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

What you're trying to do here (subscribe to a pubsub topic from code running in Cloud Functions) won't work for two reasons:

  • Cloud Functions server instances scale up and down automatically. There could be 0 instances or 1000 instances concurrently running your triggers, depending on the current load.
  • Cloud Functions shuts down running code when the function has terminated, and there is a maximum timeout of 9 minutes for any function invocation.

So, even if you manage to subscribe to a topic, that subscription doesn't have a guaranteed duration. It will eventually be destroyed, and you will lose messages.

If you want to handle messages using Cloud Functions in "project A", but the messages come from "project B", you should consider sending them from A to B, perhaps by using pubsub function in B that does nothing other that publish each message to a topic in A. You can then write another function to handle them in A.

Upvotes: 2

Related Questions