This post is sponsored by Firebase, a Node Knockout 2013 service sponsor.

Getting Started with Firebase

Firebase is a real-time backend for building modern, collaborative web and mobile applications. By replacing the traditional request / response model with a data synchronization approach, you can get apps up and running in minutes. Using Firebase as your backend, your data will be persisted in the cloud and stored as JSON. Whenever your data changes, all clients will be notified in real-time. Apps built with Firebase can be written entirely with client-side code and scale automatically.

To start storing and syncing your data, simply sign up for an account to create your first Firebase. Next, try our 5-minute chat tutorial for a quick demonstration of how Firebase works. Then check out other example apps and dive into the docs.

Integrating Firebase with Node

While you can write entire Firebase applications without any backend code, we provide a full-featured Node.js module to help you run your own servers. To get started, install Firebase via npm:

npm install firebase

Now, just require the library in your application and start storing your data:

var Firebase = require('firebase');
var myRootRef = new Firebase('');
myRootRef.set("hello world!");

All data in Firebase can be referenced by a URL. To read and write data to your Firebase, create a reference to the root of your Firebase. You can see all of your data change in Forge, our graphical debugger. Access forge by navigating to the URL of your Firebase.

Writing and Reading Data with Firebase

Writing Data

The basic Firebase write operation is a “set”, which overwrites any data at the specified location. The example below demonstrates setting the first and last names of user ‘fred’.

// First we get a reference to the location of the user’s name data:
var nameRef = new Firebase('');

// And then we write data to his first and last name locations:

In addition to strings, you can write booleans, numbers, and objects to any Firebase location. When a javascript object is written, the object properties are automatically mapped to Firebase child locations in a nested fashion: “” maps to the child “foo/bar”, “” maps to the child “foo/bar/baz” and so on. For example, the code below sets the first and last name of user ‘fred’, just as the previous code example did:

nameRef.set({first : 'Fred', last : 'Swanson'});

Reading Data

Because Firebase is a real-time database, data is never read synchronously. Instead, you read data by attaching a callback to a Firebase reference as shown:

var dataRef = new Firebase('');
dataRef.on('value', function(snapshot) {
  alert('fred’s first name is ' + snapshot.val());

In Firebase, all callbacks are asynchronous. They are triggered both for the initial state of your application and again when data changes. Callbacks receive snapshots of data, which is a picture of the data at a particular Firebase location at a single point in time.

There are 5 different event types for which you can attach callbacks:

  1. value: used to read the entire contents of a Firebase location
  2. child_added: used when retrieving a list of items in Firebase. It fires once for each immediate child of data, and continues to trigger whenever new children are added
  3. child_changed: triggered any time a child or one of its descendents changes
  4. child_removed: triggered when an immediate child is removed
  5. child_moved: used when working with Ordered Data

Check out the documentation for a full explanation on how to attach callbacks to each event type.

Authenticating Users

There are two ways to implement a user login system with Firebase.

Custom Login

If you have your own server and like to control your own authentication, or you wish to integrate existing authentication mechanisms with Firebase, you can integrate your authentication with Firebase by generating your own JSON Web Tokens (JWT). We have a Node.js helper library for generating tokens with Node.

Once you have generated a valid JWT, you can use any Firebase reference to authenticate using the auth API.

var dataRef = new Firebase("");
// Log me in.
dataRef.auth(AUTH_TOKEN, function(error) {
  if(error) {
    console.log("Login Failed!", error);
  } else {
    console.log("Login Succeeded!");

Authenticating on any reference will authenticate that client to the entire Firebase, and Firebase will seamlessly handle authenticating again if its internet connection is ever lost, so you’ll only need to perform the operation once in your app. To change a client’s credentials (for example, when a user logs in to a different account), simply re-authenticate with a new token. Read the documentation on custom token generation for a full explanation on how this works.

Simple Login

Firebase Simple Login is a built-in service that allows you to authenticate users with only client-side code. It supports Facebook, Twitter, GitHub, Persona, and email / password authentication. To get started, simply include the Firebase Simple login library in your app’s <head> tag and follow the sample code in the documentation here.

Firebase @ Node Knockout

For any questions on apps you’re building during the hackathon, email or tweet @Firebase on Twitter. You should also search our Google Group mailing list to see if a similar question has been asked by our community.

We’re giving away Firebase credit for awesome apps built during Node Knockout! Winners will receive six months of our Bonfire plan, a $900 value. The Bonfire plan entitles users to 750 concurrent connections, 75 GB of data transfer, and 10 GB of data storage.

Start using Firebase before the hackathon by going through our five minute tutorial and signing up for a free Development account.

Blog comments powered by Disqus