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

shi*_*t66 2 firebase google-cloud-pubsub google-cloud-pubsub-emulator

我正在尝试在本地使用 pubsub 运行 firebase 函数。

\n

已配置模拟器firebase init emulators

\n

firebase emulators:start --only pubsub工作正常,我可以看到日志:

\n
\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 Emulator \xe2\x94\x82 Host:Port      \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82 Pub/Sub  \xe2\x94\x82 localhost:8085 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
"pubsub": {\n  "host": "localhost",\n  "port": 8085\n},\n
Run Code Online (Sandbox Code Playgroud)\n

导出 pubsub 处理函数:

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

我运行 firebase 函数:firebase serve --only functions

\n

此行出现在控制台输出中:

\n
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."}}\n
Run Code Online (Sandbox Code Playgroud)\n

这意味着功能已找到,但由于某种原因,尽管进行了所有配置,但 firebase 仍无法连接到 pubsub 模拟器。

\n

问题是:\n如何在本地计算机上测试 pubsub 和 firebase 功能?

\n

shi*_*t66 5

经过几个小时的努力,我终于成功地使 pubsub 模拟器工作了。

firebase 文档firebase serve中没有任何内容,所以我猜这是一个已弃用的命令。我所找到的只是.firebase emulators:start

我按照为模拟功能设置管理员凭据:部分中的说明进行操作。创建一个密钥并下载 json,在终端中导出变量并运行firebase emulators:start

这有效,我可以看到我的登录输出。

无法确定问题是什么,但看起来您无法firebase serve并由于某种原因与模拟器取得联系。如果您希望它们能够相互协作,则必须运行所有模拟器。

笔记:

  1. 如果您使用的是打字稿,请不要忘记运行npm run build
  2. 创建主题时,projectId 选项应该足够了:
const pubsub = new PubSub({
  projectId:  "your-test-project",
});
const topic = pubsub.topic("test-pubsub");
Run Code Online (Sandbox Code Playgroud)