Firebase 数据库丢失 websocket 连接,导致延迟

2023-01-28前端开发问题
12

本文介绍了Firebase 数据库丢失 websocket 连接,导致延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我们在 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 连接,导致延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13