您是否可以在没有服务器的情况下通过firebase事件触发Google Cloud Functions?

时间:2016-11-05 12:33:13

标签: firebase google-cloud-platform

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

e.g。 https://github.com/firebase/flashlight

但是,如果我可以通过firebase节点中的插入触发谷歌云功能,那就太棒了。我看到谷歌云功能有各种各样的触发器:pub子,存储和直接...这些可以在没有中间服务器的情况下连接到firebase节点事件吗?

3 个答案:

答案 0 :(得分:12)

firebaser here

我们刚刚发布了Cloud Functions for Firebase。这样,您就可以在Google服务器上运行JavaScript功能,以响应Firebase事件(例如数据库更改,用户登录等等)。

答案 1 :(得分:4)

我相信您正在寻找适用于Firebase的云功能。 以下是一些链接:

答案 2 :(得分:0)

是的,您可以在没有服务器的情况下通过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);
            });
        });
相关问题