问题 :
如何设计一个以 Firebase 作为后端、可扩展的社交网络“提要”?
可能的答案:
“MVP”解决方案是设计一个feeds
root 子级,每个用户一个,并在每个关注者的提要中附加来自关注用户的任何新帖子。
users
user1
name: bob
user2
name: alice
follows:
user1: true
posts
post1
author: user1
text: 'Hi there'
feeds
user2
post1: true
这效果很好,并且在Firefeed
项目。但它的扩展性并不好:如果凯蒂·佩里想要发布一些东西,她的手机将不得不写入数百万个提要。
因此报告中的解决方案这个问题 https://stackoverflow.com/questions/27544325/firebase-data-structure-is-the-firefeed-structure-relevant将此操作委托给基于服务器的进程。
我的问题是,Firebase 是一个“无后端”解决方案,这是我使用它的主要原因,所以我想确保在没有服务器的情况下绝对不可能实现此功能。
如果feeds
孩子在上面的模式中被删除了?
然后执行以下操作:
baseRef.child('posts')
.orderBy('author')
.whereIn(baseRef.child('users/user2/follows').keys())
很遗憾,whereIn
Firebase API 中不存在,也不存在子查询:(
是否有其他不需要服务器的模型结构?
Thanks
Firebase 人员在他们的博客上回复道:https://www.firebase.com/blog/2015-10-07-how-to-keep-your-data-concient.html https://www.firebase.com/blog/2015-10-07-how-to-keep-your-data-consistent.html
这篇文章是关于“数据扇动”(在一个原子写入操作中将项目分散到多个节点)。
该技术极大地解决了原始问题的提要模型
该帖子实际上包含实现它的示例代码:
-
创建扇出对象的函数(实际上是一个简单的对象,其键是要写入的 API 端点)
function fanoutPost({ uid, followersSnaphot, post }) {
// Turn the hash of followers to an array of each id as the string
var followers = Object.keys(followersSnaphot.val());
var fanoutObj = {};
// write to each follower's timeline
followers.forEach((key) => fanoutObj['/timeline/' + key] = post);
return fanoutObj;
}
-
以及使用该函数的逻辑:
var followersRef = new Firebase('https://<YOUR-FIREBASE-APP>.firebaseio.com/followers');
var followers = {};
followersRef.on('value', (snap) => followers = snap.val());
var btnAddPost = document.getElementById('btnAddPost');
var txtPostTitle = document.getElementById('txtPostTitle');
btnAddPost.addEventListener(() => {
// make post
var post = { title: txtPostTitle.value };
// make fanout-object
var fanoutObj = fanoutPost({
uid: followersRef.getAuth().uid,
followers: followers,
post: post
});
// Send the object to the Firebase db for fan-out
rootRef.update(fanoutObj);
});
注意:这比每次在一个关注者提要中循环写入更具可扩展性。然而,对于数百万粉丝来说,这仍然是不够的。在这种情况下,信任进行多次写入的服务器操作会更安全。我认为客户端最多可以用于数百个关注者,这是社交媒体上关注者的平均数量。 (不过这个还需要通过测试来验证)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)