问题描述
我们在 Electron 应用程序中使用 Firebase 实时数据库.身份验证后第一次执行设置"或更新"完美无缺.但是,在等待一段时间(空闲 > 1 分钟)后,执行更新或设置操作会延迟 30 秒,最多 2 分钟.
We are using Firebase Realtime Database in our Electron application. Executing "set" or "update" the first time after authentication works flawlessly. However, after waiting some time (idle > one minute), executing a update or set operation is delayed by 30 seconds up to 2 minutes.
我们正在执行以下代码:
We are executing the following snipped in a promise:
this.$fbDb.ref().update(updatedNodes).then(() => {
console.log('Successfully created configuration.')
resolve()
}).catch((err) => {
reject(err)
})
firebase 日志显示Websocket 连接已断开".请看以下日志,注意18:21:12.226
之前的时间延迟(46秒):
The firebase log shows that the "Websocket connection was disconnected".
Please see the following log and pay attention to the delay (46 seconds) in time before 18:21:12.226
:
18:20:26.064 Send.vue?6513:300 Adding the configuration...
18:20:26.065 firebase.js?663c:26 [FIREBASE] 0: update {"path":"/","value":{"/surveys/65/-LDggpvburpfAYAPubqD":{...}}}
18:20:26.091 firebase.js?663c:26 [FIREBASE] event:
18:20:26.092 firebase.js?663c:26 [FIREBASE] event:
18:20:26.092 firebase.js?663c:26 [FIREBASE] event:
18:20:26.093 firebase.js?663c:26 [FIREBASE] event:
18:20:26.094 firebase.js?663c:26 [FIREBASE] event:
18:20:26.094 firebase.js?663c:26 [FIREBASE] event:
18:20:26.095 firebase.js?663c:26 [FIREBASE] event:
18:21:12.226 firebase.js?663c:26 [FIREBASE] c:0:0:0 Websocket connection was disconnected.
18:21:12.226 firebase.js?663c:26 [FIREBASE] c:0:0:0 WebSocket is closing itself
18:21:12.227 firebase.js?663c:26 [FIREBASE] c:0:0: Realtime connection lost.
18:21:12.227 firebase.js?663c:26 [FIREBASE] c:0:0: Closing realtime connection.
18:21:12.228 firebase.js?663c:26 [FIREBASE] c:0:0: Shutting down all connections
18:21:12.228 firebase.js?663c:26 [FIREBASE] p:0: data client disconnected
18:21:12.229 firebase.js?663c:26 [FIREBASE] p:0: Trying to reconnect in 0ms
18:21:12.229 firebase.js?663c:26 [FIREBASE] 0: onDisconnectEvents
18:21:12.230 firebase.js?663c:26 [FIREBASE] p:0: Making a connection attempt
18:21:12.232 firebase.js?663c:26 [FIREBASE] p:0: Auth token refreshed
18:21:12.237 firebase.js?663c:26 [FIREBASE] getToken() completed. Creating connection.
18:21:12.238 firebase.js?663c:26 [FIREBASE] c:0:1: Connection created
18:21:12.242 firebase.js?663c:26 [FIREBASE] c:0:1:0 Websocket connecting to wss://xxx.firebaseio.com/.ws?v=5&ls=abcde&ns=ourproject
18:21:12.843 firebase.js?663c:26 [FIREBASE] c:0:1:0 Websocket connected.
18:21:12.844 firebase.js?663c:26 [FIREBASE] c:0:1: Realtime connection established.
18:21:12.845 firebase.js?663c:26 [FIREBASE] p:0: connection ready
18:21:12.846 firebase.js?663c:26 [FIREBASE] p:0: {"r":13,"a":"auth","b":{"cred":"token"}}
18:21:12.847 firebase.js?663c:26 [FIREBASE] p:0: Listen on /surveys/65 for default
18:21:13.100 firebase.js?663c:26 [FIREBASE] p:0: {"r":14,"a":"q","b":{"p":"/surveys/65","h":"B9G3P0cJefaRilsIFiMp7NHwhYY="}}
18:21:13.102 firebase.js?663c:26 [FIREBASE] p:0: {"r":15,"a":"m","b":{"p":"/","d":{...}}}
18:21:13.115 firebase.js?663c:26 [FIREBASE] p:0: from server: {"r":13,"b":{"s":"ok","d":{"auth":{"provider":"custom","user_id":"ourUser","cid":"65","token":{"exp":1527614289,"user_id":"ourUser","cid":"65","iat":1527610689,"sub":"ourUser","aud":"ourproject","auth_time":1527610689,"iss":"https://securetoken.google.com/ourproject","firebase":{"identities":{},"sign_in_provider":"custom"}},"uid":"ourUser"},"expires":1527614289}}}
18:21:13.252 firebase.js?663c:26 [FIREBASE] c:0:1: Primary connection is healthy.
18:21:13.253 firebase.js?663c:26 [FIREBASE] p:0: from server: {"r":14,"b":{"s":"ok","d":{}}}
18:21:13.253 firebase.js?663c:26 [FIREBASE] p:0: listen response {"s":"ok","d":{}}
18:21:13.309 firebase.js?663c:26 [FIREBASE] p:0: handleServerMessage m {"p":"surveys/65","d":{...}}}
18:21:13.313 firebase.js?663c:26 [FIREBASE] p:0: from server: {"r":15,"b":{"s":"ok","d":""}}
18:21:13.314 firebase.js?663c:26 [FIREBASE] p:0: m response {"s":"ok","d":""}
18:21:13.339 Send.vue?6513:312 Successfully created configuration.
对于我们和我们的用户来说,延迟是一个非常大的问题.我们无法在 MacOS 或 Linux 上重现它.
The delay is a very big problem for us and our users. We have not been able to reproduce it on MacOS or Linux.
这里发生了什么,我们如何解决这个问题或进一步调试它?
What is happening here and how can we solve this problem or debug it further?
推荐答案
经过一些试验和研究(#1, #2),我们找到了一种解决方法",可以防止延迟:
After some trial and research (#1, #2), we have found a "workaround" that prevents the delay:
import {INTERNAL as firebaseDbInternal} from '@firebase/database'
// Force firebase database to use long polling instead of websockets
firebaseDbInternal.forceLongPolling()
警告:这似乎不是一个好的解决方案,因为在 internal.ts 状态:
Warning: This does not seem to be a good solution, because a comment in internal.ts states:
客户不应使用这些(功能),否则应注意它们随时可能损坏.
Customers shouldn't use these (functions) or else should be aware that they could break at any time.
我们很乐意提供关于更好解决方案的建议 - 只要没有更好的答案,我将接受此答案作为解决问题的最佳方法.
We would be happy for suggestions on a better solution - as long as there is no better answer, I will accept this answer as the best approach to solve the problem.
这篇关于Firebase 数据库丢失 websocket 连接,导致延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!