[英]firestore sync time between clients
I have this document structure in firestore collection 我在Firestore集合中有此文档结构
{ message: 'hello world', time: 1538398128 }
time value is set by client when it posts message with this function 使用此功能发布消息时,客户端设置时间值
moment().unix()
The problem is that my clients have different time and if I sort documents by time it is inconsistent, like this example 问题是我的客户有不同的时间,如果我按时间对文档进行排序,则不一致,例如此示例
{ message: 'aaa', time: 1538398317 } // client 1 post at 14.00:00
{ message: 'bbb', time: 1538398257 } // client 2 post at 14:00:10
and on client I see "bbb" before "aaa". 在客户端上,我在“ aaa”之前看到“ bbb”。 How can I fix this problem?
我该如何解决这个问题? I know that exists firebase.firestore.FieldValue.serverTimestamp(), but it gives me the value only after I posted the document, i can't use this on client.
我知道存在firebase.firestore.FieldValue.serverTimestamp(),但是只有在发布文档后,它才能为我提供值,我无法在客户端上使用它。 Thanks
谢谢
Well you will need to pick one timezone and use it as standard for a consistent data storage, otherwise every client will use the local one. 那么,您将需要选择一个时区并将其用作标准的一致数据存储,否则每个客户端都将使用本地时区。 You might want to use
moment-timezone
an extension for momentjs where you can specify the timezone you want to use : https://momentjs.com/timezone/ 您可能要使用
moment-timezone
作为momentjs的扩展名,您可以在其中指定要使用的时区: https ://momentjs.com/timezone/
It is not possible to perfectly sync different client devices (phones/browsers) with time, as you have no mechanism to force their clocks to be in sync. 您无法随时间完美地同步不同的客户端设备(电话/浏览器),因为您没有机制来强制其时钟同步。
To ensure a consistent ordering, you will need to use firebase.firestore.FieldValue.serverTimestamp()
, which once committed will be returned to the client. 为了确保顺序一致,您将需要使用
firebase.firestore.FieldValue.serverTimestamp()
,一旦提交,它将返回给客户端。 While you are waiting for it, you can use the client time as a fallback mechanism. 等待期间,可以将客户端时间用作后备机制。 Even better, you can reach out to some time server to validate that the local clock hasn't drifted too far from being usable.
更好的是,您可以联系某个时间服务器以验证本地时钟距离可用时钟没有太远。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.