firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.75k stars 872 forks source link

.info/connected does not change to false when offline #249

Closed hiranya911 closed 1 year ago

hiranya911 commented 6 years ago

From https://github.com/firebase/firebase-admin-node/issues/99

I've verified that this issue occurs with latest RTDB code as well (@firebase/database 0.1.2)

Environment

Debian buster Node.js v 8.5.0 firebase-admin v ^5.0.0

Steps to reproduce:

  1. Start application and wait for console log "Firebase connected!"
  2. Go offline (switch off wifi or remove lan cable etc.)

When internet connection is disabled, application should print "Firebase disconnected". It seems, it never does!

Relevant Code:

"use strict";

var path = require('path');
var admin = require("firebase-admin");

var serviceAccount = require(path.join(__dirname, "some-firebase-adminsdk-lu0x5-82c66d50f5.json"));

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://some.firebaseio.com/"
});

const database = admin.database();

var connectedRef = database.ref(".info/connected");
connectedRef.on("value", function(snap) {
  if (snap.val() === true) {
    console.log("Firebase connected!");
  } else {
    console.log("Firebase disconnected!");
  }
});
google-oss-bot commented 6 years ago

Hey there! I couldn't figure out what this issue is about, so I've labeled it for a human to triage. Hang tight.

google-oss-bot commented 6 years ago

Hmmm this issue does not seem to follow the issue template. Make sure you provide all the required information.

schmidt-sebastian commented 6 years ago

Hiranya, do you know if this a Node only issue? Our networking layer is different in Node and I wonder whether this is something we never supported (it also doesn't make as much sense in Node as it does on the mobile platforms).

mikelehen commented 6 years ago

FWIW, I would expect it to work (the websocket connection should die and trigger .info/connected to change), but it may take a really long time (e.g. 15 minutes) depending on OS TCP timeout settings, etc.

hiranya911 commented 6 years ago

I have only tested this in Node (with the Admin SDK). I think @mikelehen may be right. I remember seeing this event fire long time (> 15 mins) after a disconnect. Since the delay was long, I brushed it off as a fluke. I also agree that it is not a very popular use case for server-side environments. So may be we can just ensure it works in the web SDK, and document the behavior for Node?

schmidt-sebastian commented 6 years ago

It really depends on the underlying network stack. We have seen that this event doesn't fire for a full hour on some Android devices as the TCP retry window prevents the network stack from bubbling up these connection drops.

kkm commented 6 years ago

+1

AndryBray commented 6 years ago

I have the same after upgrading to 4.1.5 (https://firebase.google.com/support/release-notes/js) - previous versions where OK. It's like the event is fired twice:

gsushant commented 5 years ago

As @AndryBray pointed out, I am having the same issue (event firing twice - offline followed by online). The example at: https://firebase.google.com/docs/database/web/offline-capabilities#section-presence works fine with 4.1.3 but doesn't work with 4.1.4 (and 5.0.4).

DennisSmolek commented 5 years ago

For me it's never turning true... It's stuck at false..

vincentwoo commented 5 years ago

I think this is a reasonable argument for using heartbeat keepalive

DennisSmolek commented 5 years ago

Just for posterity, My issue was somewhere in the system it disconnected using goOffline to stop permissions issues from firing errors. Even after reconnecting I realized messing with the DB's connection state can cause problems with .info

ristiisa commented 5 years ago

I can confirm that it works with 4.1.3 but not with latest version (5.9.0)

jedierikb commented 5 years ago

on chrome, using 6.0.0, problem persists.

LanderBeeuwsaert commented 5 years ago

I have the same as @AndryBray : when simulating a disconnect in chrome by using devtools, I get an offline directly followed by online.

If I run:

async isConnected(): Promise { return (await database() .ref('.info/connected') .once('value')).val(); }

then it returns true, while I have in the network tab of chrome devtools that I'm offline.

version: "firebase": "6.0.2", Persistency is off.

zabaat commented 4 years ago

Having same issue as well firebase 5.10.0 when simulating a disconnect in chrome by using devtools, I get an offline directly followed by online. When i disconnect internet connection it doesn't even fire an offline.

Also occurs with firebase 6.1.1

squigglybob commented 4 years ago

+1 on firebase 6.2.0

nbransby commented 4 years ago

Also seeing this with 6.2.3, what is the plan for this issue as its been open for nearly 2 years now?

mintu19 commented 4 years ago

+1 on firebase 6.6.1 on node

kkm commented 4 years ago

+1 more than 3 still not solved.

LanderBeeuwsaert commented 4 years ago

Just encountered this again, with latest libraries. Maybe can be interesting: When I disconnect my wifi, it works fine. When I do a disconnect in chrome devtools network tab, it gives the issue.

johnnyoshika commented 3 years ago

Does this work for anyone? This is what I see:

There's some interesting insight about onDisconnected() here: https://stackoverflow.com/a/49547545/188740

norcrel commented 3 years ago

Does this work for anyone? This is what I see:

  • If I navigate away from my app or close my tab, it's a clean disconnect and the following immediately happens:

    • ref('.info/connected').on('value') === false
    • onDisconnect() gets called
  • If I set my network status to offline in dev tools, ref('.info/connected').on('value') gets called twice. First with false then with true. So even though I'm offline, it doesn't detect that.
  • If I turn off my WiFi (i.e. dirty disconnect), ref('.info/connected').on('value') === false correctly gets called immediately, but onDisconnect doesn't. It takes a few minutes before onDisconnect gets called, leaving the server thinking that the user is online when s/he isn't for up to a few minutes.

There's some interesting insight about onDisconnected() here: https://stackoverflow.com/a/49547545/188740

This is also happening to me on Chrome. Setting "offline" in dev tools on chrome also caused a false then true to be logged

oveddan commented 3 years ago

@johnnyoshika I have the same issue, where I get 2 .info/connected events, the first with value false the second with true.

johnnyoshika commented 3 years ago

@oveddan it's a very strange behaviour. I think it's a bug.

LanderBeeuwsaert commented 3 years ago

@schmidt-sebastian This seems to be a bug that has been happening for some years in a consistent manner. Is there an idea about/planning for looking into this?

b2m9 commented 2 years ago

I'm running into the same issues that toggling network status in the Chrome/Chromium DevTools (v 91.0.4472.77) fires twice and stays in the "online" state (despite network being toggled off). I'm on version 9.0.0-beta.2.

It makes debugging online/offline behavior unnecessarily complicated.

kkm commented 2 years ago

almost 4 years and still bug.

oveddan commented 2 years ago

I built a lot of my application based on this documented feature, and as a result of this bug it's lead to some pretty critical bugs happening in my application. If it's not working as documented and expected, and there doesn't seem to be a stated acknowledgement of the issue or plan to look at it, then why leave it in the documentation?

Rubilmax commented 2 years ago

If you are using React: it only works when inside a useEffect hook.

React.useEffect(() => {
  database.ref('.info/connected').on('value', (snapshot) => {
    const isOnline = snapshot.val();
  });
}, []);

I'd also advise to use a lodash/debounce to avoid false triggers

johnnyoshika commented 2 years ago

@Rubilmax, which part are you referring to when you say it works inside of useEffect? I experienced several problems with .info/connected that I outlined here: https://github.com/firebase/firebase-js-sdk/issues/249#issuecomment-791199152, but I don't think useEffect will change anything. Are you seeing something different?

Rubilmax commented 2 years ago

@Rubilmax, which part are you referring to when you say it works inside of useEffect? I experienced several problems with .info/connected that I outlined here: #249 (comment), but I don't think useEffect will change anything. Are you seeing something different?

I am referring to how .info/connecteed is expected to work. Respectively, I tested (locally, using the firebase emulators and I'm using it production now) your 3 use cases with the listener added inside a useEffect and using a debounce of 2000ms and the results were the following:

I did not test any feature related to onDisconnect

I originally posted my comment because at first I was going to register the listener at the root of my app: I had not results and that's the reason I came upon this issue. However, after having moved the listener registering part inside a useEffect in the root component of my app, it's working just as expected.

johnnyoshika commented 2 years ago

@Rubilmax Thanks. Use case 1 and 3 that you're seeing is the same behavior that I saw for .info/connected. You're seeing a different behavior for use case 2 with dev tools and that's interesting. My .info/connected would get called twice when I went offline in dev tools: once with online==false and once for online==true even if I left it in offline mode.

I still believe there's a problem with onDisconnect.

Note:

Ajiharan commented 2 years ago

I have the same situation here. info/connected events called with the first with value false the second with true, then I set my network offline through chrome dev tools .info/connected called twice with value false and the second with true. when I disconnect my wifi .info/connected called with value false as expected but onDisconnect doesn't call after my wifi disconnected

firebase version: 8.9.1

vasujogani commented 2 years ago

Any solution to this issue? The issue is getting connected=false, and connected=true right after. It also seems like none of the firebase calls (.set(), .update()) go through once I get this issue.

maneesht commented 1 year ago

@vasujogani are you still having issues on the latest version of the JS SDK?

google-oss-bot commented 1 year ago

Hey @hiranya911. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 1 year ago

Since there haven't been any recent updates here, I am going to close this issue.

@hiranya911 if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.