17

I am trying to set up a scheduled function in Firebase Cloud Functions. As a simple test, I have tried to recreate the sample shown on the documentation page:

const functions = require('firebase-functions')

exports.scheduledFunction = functions.pubsub
  .schedule('every 5 minutes')
  .onRun(context => {
    console.log('This will be run every 5 minutes!')
    return null
  })

However, when I run firebase serve --only functions, I get the following error:

function ignored because the pubsub emulator does not exist or is not running.

Any idea why I get this message and how I can fix it?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Moshe
  • 6,011
  • 16
  • 60
  • 112
  • This is NOT a duplicate question. This question is asking about why I cannot run a scheduled cloud function with the `firebase serve` command. The other question is asking about how to run a scheduled cloud function with an api call. – Moshe Dec 15 '19 at 18:35
  • I should note that I would still like an answer to this question -- even though the other question was properly answered. As I noted in my previous comment, they are asking two different things. As such, can you please reopen this question? Thanks. – Moshe Dec 15 '19 at 18:40
  • Done. Sorry about the mistake, as it's indeed clearly a different question than the one I linked. – Frank van Puffelen Dec 15 '19 at 21:03
  • Thanks -- and no worries. I realize that the titles were very similar -- I should have been clearer in my title. – Moshe Dec 16 '19 at 16:54

2 Answers2

16

From the documentation on Firebase's local emulator:

The Firebase CLI includes a Cloud Functions emulator which can emulate the following function types:

  • HTTPS functions
  • Callable functions
  • Cloud Firestore functions

So the local Firebase emulators don't currently support pubsub, and the error message seems to confirm that. So for the moment, you can't run pubsub triggered Cloud Functions locally.

A feature request for adding PubSub support to the emulator was filed. You might want to read up (and possibly comment) there, as the direction taken may or may not match with your needs.

The local shell does support invoking pubsub functions. That is of course quite different, but might be useful as a workaround for the moment.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
9

For what it is worth, you need to enable the pubsub emulator in firebase. Add this to your emulators block:

{
  "emulators": {
    "pubsub": {
      "port": 8085
    },
  }
}

Even then, it only creates the definition. The emulator doesn't support running the function on a schedule.

To simulate that behavior, I define a HTTP trigger, in which I manually send a message to the topic. For a schedule topic, it is firebase-schedule-<functionName>. In your case it will be firebase-schedule-scheduledFunction.

Sample code looks like:

const pubsub = new PubSub()

export const triggerWork = functions.https.onRequest(async (request, response) => {
  await pubsub.topic('firebase-schedule-scheduledFunction').publishJSON({})
  response.send('Ok')
})

Then on the command line, I trigger the HTTP function on a schedule.

while [ 1 ];
  do wget -o /dev/null -O /dev/null http://localhost:5001/path/to/function/triggerWork;
  sleep 300;
done
Mere Vicharr
  • 171
  • 2
  • 1
  • This was really helpful for me! For anyone else who is having trouble with it, the new convention for the pubsub topic is firebase-scheduled-- e.g. firebase-scheduled-scheduledFunctionCrontab-us-east1. https://firebase.google.com/docs/functions/schedule-functions – dnishiyama Jun 20 '21 at 17:36
  • What file should I edit for emulators/pubsub setting? – Mark Choi Aug 08 '21 at 06:48
  • @MarkChoi and anyone else wondering which file to edit, you should add it on firebase.json file – Rasheek Mohamed Nov 23 '21 at 09:11