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


A Firebase reference represents a particular location in your database and can be used for reading or writing data to that database location.

Your initial Firebase reference will be constructed from a full, secure Firebase database URL (e.g., but you can subsequently reference child locations by calling child() on an existing Firebase reference. See Creating Firebase References for more details.

Writing is done with set() and reading can be done with on() (see Writing Data with set() and Reading Data).


Traverse to a database location (using new Firebase() and child()), and write some data.

var sampleChatRef = new Firebase('');
var fredNameRef = sampleChatRef.child('users/fred/name');
fredNameRef.set({ first: 'Fred', last: 'Flintstone' });

Now read the data back (and get notified whenever it changes).

fredNameRef.on('value', function(nameSnapshot) {
  var y = nameSnapshot.val();
  // y now contains the object { first: 'Fred', last: 'Flintstone' }.

Let's add a new chat message child to the message_list location using push().

var messageListRef = sampleChatRef.child('message_list');
messageListRef.push({ 'user_id': 'fred', 'text': 'Yabba Dabba Doo!' });

And let's listen for new children added to the message_list location using on(). We'll be notified of our 'Yabba Dabba Doo!' message as well as any other messages that were added in the past, and any new messages that get added in the future.

messageListRef.on('child_added', function(newMessageSnapshot) {
  var userId = newMessageSnapshot.child('user_id').val();
  var text = newMessageSnapshot.child('text').val();
  // Do something with user_id and text.