Pregunta

I need to implement a listener that writes to my firebase once it is triggered from an outside message. Running an external server for two pages of code seems unreasonable to me, and I've read about Firebase's static hosting.

Can small custom scripts be run on Firebase like it is possible on some other BaaS like Backendless?

¿Fue útil?

Solución

Since March 2017 it is possible to run small snippets of JavaScript code on Google's servers in response to events in Firebase by using Cloud Functions for Firebase. Triggering a function by a write to the database can be as simple as:

// Listens for new messages added to /trigger
exports.onTrigger = functions.database.ref('/trigger')
    .onWrite(event => {
      // Grab the current value of what was written to the Realtime Database.
      const original = event.data.val();
      // TODO: write value to data and return a promise
    });

I highly recommend reading the documentation and checking out the page with common use-cases for Cloud Functions for Firebase.

Old answer

You cannot execute your own code on Firebase's servers. But instead you could have the trigger flow through Firebase and then writes the update to the Firebase.

var trigger = new Firebase('http://yourfirebase.firebaseio.com/trigger');
var data = new Firebase('http://yourfirebase.firebaseio.com/data');
handlersRef.on('value', function(snapshot) {
    // write value to data
});

Of course this requires an active/connected client, instead of the server you're trying to avoid.

If you don't want that, you will need a server. There are nowadays plenty of free and reasonably priced options available for that.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top