简体   繁体   English

Firebase 函数不会看到 pubsub 模拟器在本地运行

[英]Firebase functions won't see pubsub emulator running locally

I'm trying to run firebase functions with pubsub locally.我正在尝试在本地使用 pubsub 运行 firebase 函数。

Have configured emulators with firebase init emulators .已使用firebase init emulators配置了仿真器。

firebase emulators:start --only pubsub works properly, I can see log: firebase emulators:start --only pubsub工作正常,我可以看到日志:

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

pubsub emulator config in firebase.json: firebase.json 中的 pubsub 模拟器配置:

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

A pubsub handler function is exported:一个 pubsub 处理程序 function 被导出:

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我运行 firebase 功能: firebase serve --only functions

This line appears in console output:此行出现在控制台 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.这意味着找到了 function 但由于某种原因 firebase 无法连接到 pubsub 模拟器,尽管有所有配置。

And here is the question: How to test pubsub and firebase functions on local machine?这里是一个问题:如何在本地机器上测试 pubsub 和 firebase 功能?

After many hours of struggling I finally managed to make pubsub emulator work.经过数小时的努力,我终于设法使 pubsub 模拟器工作。

There is nothing about firebase serve in firebase docs so I guess it's a deprecated command. firebase 文档中没有关于firebase serve的内容,所以我想这是一个不推荐使用的命令。 All I found was firebase emulators:start .我发现的只是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 .创建一个密钥并下载 json,在终端中导出变量并运行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.不能确定是什么问题,但看起来你不能firebase serve并与模拟器取得联系。 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如果您使用的是 typescript,请不要忘记运行npm run build
  2. projectId option should be enough when you create a topic:创建主题时,projectId 选项应该足够了:
const pubsub = new PubSub({
  projectId:  "your-test-project",
});
const topic = pubsub.topic("test-pubsub");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 pubsub 模拟器在本地调用 firebase 计划函数 - How to invoke firebase Schedule functions locally using pubsub emulator Firebase 模拟器:在函数中使用 PubSub - Firebase Emulator: using PubSub in Functions 如何使用 firebase 模拟器 pubsub 在本地测试定时功能? - How to use firebase emulators pubsub to test timed functions locally? Firebase 模拟器云功能 + PubSub 订阅生产主题 - Firebase Emulator Cloud Functions + PubSub subscribe to production topics 如果没有完全部署,为什么Angular Firebase Cloud Functions Emulator无法更新功能? - Why won't Angular Firebase Cloud Functions Emulator update functions without a full deploy? 使用 pubsub 重试 Firebase 函数 - Retry on Firebase functions with pubsub 使用安全连接 (HTTPS) 在本地运行 Firebase 函数 - Running Firebase Functions locally with secure connections (HTTPS) 具有本地运行的谷歌功能的谷歌 firebase 数据库 - Google firebase database with google functions running locally 有没有办法向正在运行的 firebase 仿真器添加/修改功能? - Is there a way to add/modify functions to a running firebase emulator? 在本地测试 Cloud Functions 时 Cloud Firestore 模拟器未运行 - Cloud Firestore emulator not running when testing Cloud Functions locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM