Codementor Events

A Little Kindling for Your Firebase

Published Jun 16, 2017
A Little Kindling for Your Firebase

Firebase is a database that I’ve avoided for a long time. I believed that the more specialized, optimized, and scalable the database was, the better. But I met a user on Codementor that reminded me the beauty and importance of simplicity.

Pros

The Firebase Console

PhpMyAdmin, an open source administration tool, was wonderful for understanding how my tables were structured and modifying data directly. Since alternative databases are trendy, many don't use anything with the same level of GUI control. But within the Firebase console, you can view the following:

Screen Shot 2017-06-09 at 6.39.34 PM.png

Firebase was Made for Live Environments

In today's UI world, it is important and convenient to have a database that reacts to events, immutable states, and services. With Redux, it becomes easy to set up Firebase to directly set giant objects with new values. Or if you're listening to single values, that too can be done.

Another advantage is that Firebase was made as a minimum viable product, so the setup is incredibly simple.

The Firebase Ecosystem:

  • Hosting with Firebase app
  • Lambdas with Firebase functions
  • Notifications with Firebase cloud messaging
  • KV database with Firebase
  • Blobs with Firebase storage

Cons

Filtering

This has been one of my biggest gripes and has led me to believe I may be better off implementing a pub/sub with Firebase functions rather than doing it in the client. The filtering and sorting is relatively simple and only works on single keys.

Joining

More often than not, you will want to specify keys (and maybe a summary) of other items on an item, then make additional Firebase requests as you need them.

Pagination

Say I have a list that I want to be kept up to date with another list:

var listRef = firebase.database().ref("/my-list");
listRef.on("value", function(snapshot){
  handleItems(snapshot.val());
});

Simple enough. But I only want to subscribe to them 10 at a time:

var currentNum = 0;
var prevRef = null;
var refs = [];
var totalSnapShot = {};
var lastKey = null;
function nextPageRef(cb){
  var ref = firebase.database().ref("/my-list")
  if(prevRef){
    prevRef.off("value");
  }
  currentNum += 10;
  prevRef = ref.limitToFirst(currentNum);
  return prevRef;
}

However, the above code doesn't consider how you want to deal with duplicate values, removals, or updates. It simply provides you a list with the next ten.

Iteration

firebase.database().ref("/some-page").on((snapshot)=>{
  var value = snapshot.val();
  handleArray(Object.keys(value).map((k)=> value[k]));
})

Almost every time I want to use a list as a list, this is a must. Lodash's support for objects looks much more appealing when the code starts to be to filled with this boilerplate code.

Having the Automated Key Available on Each Item

var ref = firebase.database().ref("/some-page").push({})
ref.set({
  key: ref.key
});

I haven't looked into if this makes two HTTP calls or just one, but this has become quite normal for when I am helping others.

Additional Resources

Discover and read more posts from Sam Tobia
get started
post comments3Replies
Rob Wilkinson
7 years ago

Also, Firebase static hosting is awesome, fast and free :)

Frank van Puffelen
7 years ago

I haven’t looked into if this makes two HTTP calls or just one

When you perform client-side joins with one of the Firebase SDKs, all calls go over a single (WebSocket) connection. So they all go over the same connection and are automatically kept in the same sequent. For some more background on this, see my explanation on it on Stack Overflow: http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786

Vincent Gauthier
8 years ago

WOOOOO