问题描述
我需要实现一个侦听器,一旦从外部消息触发,就会写入我的Firebase。运行外部服务器的两页代码似乎对我来说是不合理的,我已经阅读了Firebase的静态托管。
可以在Firebase上运行小型自定义脚本可能在一些其他BaaS像Backendless?
从2017年3月开始 可以运行小片段在Google服务器上使用响应Firebase中的事件。通过写入数据库来触发函数可以很简单:
$ p $ //监听添加到/ trigger $的新消息b $ b exports.onTrigger = functions.database.ref('/ trigger')
.onWrite(event => {
//获取写入实时数据库的当前值。
const original = event.data.val();
// TODO:向数据写入值并返回一个promise
});
强烈建议您阅读文档并使用。
旧答案
您无法在Firebase的服务器上执行自己的代码。但是,您可以让触发器流经Firebase,然后将更新写入Firebase。
$ b
var trigger = new Firebase(' http://yourfirebase.firebaseio.com/trigger');
var data = new Firebase('http://yourfirebase.firebaseio.com/data');
handlersRef.on('value',function(snapshot){
//写入数据
});
当然这需要一个主动/连接的客户端,而不是你想要避免的服务器。
如果你不需要,你需要一个服务器。现在有很多免费和合理的价格可供选择。
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?
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.
这篇关于我可以在Firebase上托管侦听器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!