【问题标题】:Can you trigger a Google Cloud Functions via firebase event without a server?您可以在没有服务器的情况下通过 firebase 事件触发 Google Cloud Functions 吗?
【发布时间】:2017-04-17 20:21:34
【问题描述】:

我将在我的 Firebase 应用程序旁边实现一个弹性搜索索引,以便它可以更好地支持临时全文搜索和地理搜索。因此,我需要将 firebase 数据同步到弹性搜索索引,并且所有示例都需要一个服务器进程来侦听 firebase 事件。

例如https://github.com/firebase/flashlight

但是,如果我可以通过在 firebase 节点中插入来触发谷歌云功能,那就太好了。我看到谷歌云功能有各种触发器:发布订阅、存储和直接......这些中的任何一个都可以在没有中间服务器的情况下桥接到 firebase 节点事件吗?

【问题讨论】:

    标签: firebase google-cloud-platform


    【解决方案1】:

    firebaser 在这里

    我们刚刚发布了Cloud Functions for Firebase。这允许您在 Google 的服务器上运行 JavaScript 函数以响应 Firebase 事件(例如数据库更改、用户登录等等)。

    【讨论】:

    • 酷,会检查一下 - 你有幻灯片的链接吗?我在 github 自述文件中没有看到它
    • 希望看到这个实现,以及从 Firebase Web 应用程序读取/写入 DataStore 的官方方式(不需要实时的长期数据库存储)
    • 看来GCN 17 上会宣布一些有趣的事情。 :)
    • @Frank 如果我们的服务器不支持 Npm,我们还有其他选择吗
    • 云函数在 Google 的服务器上运行,而不是在您的服务器上。
    【解决方案2】:

    我相信 Cloud Functions for Firebase 正是您想要的。 这里有几个链接:

    【讨论】:

    • 是的,我想就是这样 - 他们一定是最近才发布的?
    • @MonkeyBonkey 是的 - 视频来自昨天。我已经更新了我的答案,包括在 Google Next 上的演讲 :)
    【解决方案3】:

    是的,您可以在没有服务器的情况下通过 firebase 事件触发 Google Cloud Functions。 根据文档,例如,Firebase 允许您在用户写入 firebase 数据库时使用云功能发送通知。

    为此,我必须编写如下的 javascript

    'use strict';
    
    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);
    
    exports.sendNotification = functions.database.ref('/articles/{articleId}')
            .onWrite(event => {
    
            // Grab the current value of what was written to the Realtime Database.
            var eventSnapshot = event.data;
            var str1 = "Author is ";
            var str = str1.concat(eventSnapshot.child("author").val());
            console.log(str);
    
            var topic = "android";
            var payload = {
                data: {
                    title: eventSnapshot.child("title").val(),
                    author: eventSnapshot.child("author").val()
                }
            };
    
            // Send a message to devices subscribed to the provided topic.
            return admin.messaging().sendToTopic(topic, payload)
                .then(function (response) {
                    // See the MessagingTopicResponse reference documentation for the
                    // contents of response.
                    console.log("Successfully sent message:", response);
                })
                .catch(function (error) {
                    console.log("Error sending message:", error);
                });
            });
    

    【讨论】:

      猜你喜欢
      • 2016-05-26
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 2019-12-02
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多