njho
njho

Reputation: 2158

Viewing/Managing number of Firebase listeners in React Application

I have a React Redux application that will have listeners on multiple Firebase Endpoints. These listeners will be mounted and unmounted at various locations on the application and I intend on these being dynamically generated based on database changes.

Questions

  1. Can I view the number of active listeners on the firebase object?
  2. In the code below, var firebaseRef is a reference to the actual firebase object correct? Therefore, all listeners are really on the firebase object and will not be destroyed if the component unmounts without calling .off()?
  3. Should I worry about having ~10 active listeners?
  4. Does anyone know if not using .off() on database references will break code? Or that it will significantly alter performance?

The Firebase Documentation has it mounted in ComponentWillMount

componentWillMount: function() {
var firebaseRef = firebase.database().ref('todoApp/items');
this.bindAsArray(firebaseRef.limitToLast(25), 'items');
}

and removed at ComponentWillUnmount

componentWillUnmount: function() {
  this.firebaseRef.off();
}

Thanks!

Upvotes: 2

Views: 896

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598951

  1. Can I view the number of active listeners on the firebase object?

There is no API that returns the number of listeners active in a client. But you can easily just keep a counter yourself.

  1. In the code below, var firebaseRef is a reference to the actual firebase object correct? Therefore, all listeners are really on the firebase object and will not be destroyed if the component unmounts without calling .off()?

A Reference is really just the information about a location in the database. Keeping them around is very cheap.

  1. Should I worry about having ~10 active listeners?

The number of listeners is not a big deal. What you should consider is the amount of data you're reading. In general I recommend to only synchronize data that you are (either actively or frequently) showing to the user.

  1. Does anyone know if not using .off() on database references will break code? Or that it will significantly alter performance?

Calling .off() or not does not break the code. But once you attach a listener to a location with on() the data for that location will be synchronized until you call off(). Having lots of lingering listeners throughout your app may lead to reading lots of data that your user doesn't see.

Upvotes: 2

Related Questions