MonkeyBonkey
MonkeyBonkey

Reputation: 47871

Can you trigger a Google Cloud Functions via firebase event without a server?

I will be implementing an elastic search index alongside my firebase application so that it can better support ad-hoc full text searches and geo searches. Thus, I need to sync firebase data to the elastic search index and all the examples require a server process that listens for firebase events.

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

However, it would be great if I can just have a google cloud function triggered by an insert in a firebase node. I see that google cloud functions has various triggers: pub sub, storage and direct... can any of these bridge to a firebase node event without an intermediate server?

Upvotes: 3

Views: 2987

Answers (3)

Nilesh
Nilesh

Reputation: 1009

yes, you can trigger a Google Cloud Functions via firebase event without a server. As per documents,Firebase allows for example you can send notifications using a cloud function when a user write into firebase database.

For that, I had to write a javascript as below

'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);
            });
        });

Upvotes: 0

bastien
bastien

Reputation: 2999

I believe Cloud Functions for Firebase are what you are looking for. Here are a few links:

Upvotes: 4

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

firebaser here

We just released Cloud Functions for Firebase. This allows you to run JavaScript functions on Google's servers in response to Firebase events (such as database changes, users signing in and much more).

Upvotes: 11

Related Questions