简体   繁体   中英

Firebase functions won't see pubsub emulator running locally

I'm trying to run firebase functions with pubsub locally.

Have configured emulators with firebase init emulators .

firebase emulators:start --only pubsub works properly, I can see log:

┌──────────┬────────────────┐
│ Emulator │ Host:Port      │
├──────────┼────────────────┤
│ Pub/Sub  │ localhost:8085 │
└──────────┴────────────────┘

pubsub emulator config in firebase.json:

"pubsub": {
  "host": "localhost",
  "port": 8085
},

A pubsub handler function is exported:

exports.testPubsub = functions.pubsub.topic("test-pubsub").onPublish(async (message) => {
    console.log(`test event received by pubsub handler: ${message.json}`);
});

I run firebase functions with: firebase serve --only functions

This line appears in console output:

functions[pubsub-testPubsub]: function ignored because the pubsub emulator does not exist or is not running. {"metadata":{"emulator":{"name":"functions"},"message":"function ignored because the pubsub emulator does not exist or is not running."}}

Which means function was found but for some reason firebase cannot connect to pubsub emulator, despite all the configs.

And here is the question: How to test pubsub and firebase functions on local machine?

After many hours of struggling I finally managed to make pubsub emulator work.

There is nothing about firebase serve in firebase docs so I guess it's a deprecated command. All I found was firebase emulators:start .

I followed instructions in To set up admin credentials for emulated functions: section. Created a key and downloaded json, exported variable in terminal and ran firebase emulators:start .

That worked and I can see my log in outputs.

Can't tell for sure what was the problem, but looks like you cannot firebase serve and get in touch with emulator for some reason. You have to run all the emulators if you want them to work with each other.

Notes:

  1. if you are using typescript, don't forget to run npm run build
  2. projectId option should be enough when you create a topic:
const pubsub = new PubSub({
  projectId:  "your-test-project",
});
const topic = pubsub.topic("test-pubsub");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM