Firebase侦听器是如何工作的? [英] How firebase listener actually work?

查看:150
本文介绍了Firebase侦听器是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上想知道,当你向数据库添加一个监听器时,底层的实际机制是如何工作的。是更频繁拉或其他特别的东西?

更新:
为了使其更清楚,我明白了听者是什么,但我的意思是听工作,客户端(Android)如何知道服务器上的数据已更改。这只是一个周期性的拉动? (Firebase工程师已经做了很多努力来覆盖这一点,并使我们更容易)。

看起来firebase不是开放源代码。

  //附加一个监听器来读取我们帖子中的数据参考
ref.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
System.out.println(snapshot.getValue());
}
@Override $ b $ public void onCancelled(FirebaseError firebaseError ){
System.out.println(The read failed:+ firebaseError.getMessage());
}
});


解决方案

如何在写作的时候工作。当您的应用程序连接到Firebase数据库时,它会打开一个从设备到一个Web套接字连接到一个Firebase服务器。此连接在您的应用程序的整个生命周期中保持打开状态,或直到您调用 goOffline()



侦听器,客户端将位置(和潜在的查询参数)发送到服务器。服务器将该侦听器添加到所有连接客户端的所有侦听器的列表中。然后,它还会发回该侦听器的初始数据。

只要将写操作提交给数据库,服务器就会扫描侦听器。对于每个相关的侦听器,服务器通过打开的Web套接字向客户端发送更新。


I'm wondering on Android, how the underlying actual mechanism work when you add an listener to the database. Is it just more frequent pulling or something else special?

Update: To make it clearer, I understand what a listener is, but I meant how does the 'listening' scheme work, how a client (Android) knows the data on the server changed. Is it just a periodical pulling? (and Firebase engineers already do the hard work to cover that and make it easy for us).

Looks like firebase is not open-source.

// Attach an listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        System.out.println(snapshot.getValue());
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
});

解决方案

disclaimer: this is a simplified description of how things work at the time of writing. Things may have changed by the time your read it.

When your app connects to the Firebase Database, it opens a web socket connection from the device to a Firebase server. This connection stays open for the lifetime of your app or until you call goOffline().

When you attach a listener, the client sends the location (and potential query parameters) to the server. The server adds that listener to a list of all listeners of all connected clients. It then also sends back the initial data for that listeners.

Whenever a write operation is committed to the database, the server scans the listeners. For each relevant listener, the server sends an update to the client over the open web socket.

这篇关于Firebase侦听器是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆