Firebase database looses websocket connection whic

2020-07-11 08:26发布

问题:

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)
})

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.

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:

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()

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.