You're viewing the legacy docs. They are deprecated as of May 18, 2016.
These docs are for version 2.4.2 and below of the Javascript SDK. Go to our current docs, or see our Web migration guide.

Version 2.4.2Changelog

Firebase.on()

on() is used to listen for data changes at a particular location. This is the primary way to read data from a Firebase database. Your callback will be triggered for the initial data and again whenever the data changes. Use off( ) to stop receiving updates. See Reading Data for more details.

Value

This event will trigger once with the initial data stored at this location, and then trigger again each time the data changes. The DataSnapshot passed to the callback will be for the location at which on() was called. It won't trigger until the entire contents has been synchronized. If the location has no data, it will be triggered with an empty DataSnapshot (val() will return null).

firebaseRef.on('value', function(dataSnapshot) {
  // code to handle new value.
});

Child Added

This event will be triggered once for each initial child at this location, and it will be triggered again every time a new child is added. The DataSnapshot passed into the callback will reflect the data for the relevant child. For ordering purposes, it is passed a second argument which is a string containing the key of the previous sibling child by priority order (or null if it is the first child).

firebaseRef.on('child_added', function(childSnapshot, prevChildKey) {
  // code to handle new child.
});

Child Removed

This event will be triggered once every time a child is removed. The DataSnapshot passed into the callback will be the old data for the child that was removed. A child will get removed when either:

  • a client explicitly calls remove() on that child or one of its ancestors
  • a client calls set(null) on that child or one of its ancestors
  • that child has all of its children removed
  • there is a query in effect which now filters out the child (because it's priority changed or the max limit was hit)
firebaseRef.on('child_removed', function(oldChildSnapshot) {
  // code to handle child removal.
});

Child Changed

This event will be triggered when the data stored in a child (or any of its descendants) changes. Note that a single child_changed event may represent multiple changes to the child. The DataSnapshot passed to the callback will contain the new child contents. For ordering purposes, the callback is also passed a second argument which is a string containing the key of the previous sibling child by priority order (or null if it is the first child).

firebaseRef.on('child_changed', function(childSnapshot, prevChildKey) {
  // code to handle child data changes.
});

Child Moved

This event will be triggered when a child's priority changes such that its position relative to its siblings changes. The DataSnapshot passed to the callback will be for the data of the child that has moved. It is also passed a second argument which is a string containing the key of the previous sibling child by priority order (or null if it is the firstchild).

firebaseRef.on('child_moved', function(childSnapshot, prevChildKey) {
  // code to handle child ordering changes.
});