2
votes

I'm writing a Chrome background extension with firebase. What I've noticed is that when I hibernate my computer (Win7) while Chrome is open, the connection with firebase disconnects (as would be expected). However, when I return from hibernate it does not automatically reconnect and my .on() events are no longer firing.

I've already looked through the API documentation and this StackOverflow question about detecting disconnects: Detect if Firebase connection is lost/regained

Here's some pseudocode that I'm using to test the connection:

var myFirebase = new Firebase('https://my.firebaseio.com/'); //replace
setInterval(function() {
  myFirebase.child('.info/connected').on('value', function (snap) {
    if (snap.val() === true) {
      console.log('Connected: ' + (new Date()).toString());
    }
    else {
      console.log('Disconnected: ' + (new Date()).toString());
    }
  });
}, 5000);

This works as expected when running the javascript through a webpage - it connects, then disconnects when I hibernate, then reconnects soon after coming back from hibernation.

However, when it runs as a background page as part of a google extension, it disconnects and never reconnects.

Is there a way to force a reconnect and/or a reason why this isn't working as expected?

1
Firebase engineer here. This certainly /should/ work, but it's possible the chrome extension environment is somehow defeating our disconnect-detection logic. If it's not too much trouble, could you email us ([email protected]) with your extension (or a stripped-down version of it) so we can more easily reproduce the issue and investigate? Thanks!Michael Lehenbauer
As a side note - calling on() inside a setTimeout is probably not going to give you the behavior you want. on() attaches a callback that is fired anytime data changes. By calling on every 5 seconds, you're attaching a new callback. So after 1 minute, each time a data update occurs, you'd get 12 callbacks, after 2 minutes, 24, etc...Andrew Lee

1 Answers

0
votes

Do your permissions look something like this?

"content_security_policy": "script-src 'self' https://static.firebase.com https://myfirebase.firebaseio.com https://s-ord-nss-1.firebaseio.com https://s-ord-nss-2.firebaseio.com; object-src 'self'"

And are you getting any permission errors in the log?

(Just a hunch--no idea if this could be related to connection logic, but I've had problems with permissions in the past.)