Appbase.io News

Database service for the #realtimeweb

Building a realtime NodeJS app with Appbase

Appbase <3 NodeJS

The Appbase JavaScript library is now also available as a NodeJS package. This has a very useful effect: You can do realtime queries for your own data from NodeJS, and importantly in a truly secure environment.

This opens up some interesting use-cases. For instance,

  1. Batch processing and aggregation of data and ease of integration with an existing stack,
  2. Use Appbase in a runtime like Node-webkit to create new experiences, and
  3. Using Appbase as a data service for various sensors and IoT devices that are node compatible, like Tessel.

The biggest gain for us has been the streamlining of our development flow. We now build using NodeJS and it's tooling, and use browserify to produce the client Javascript library. This is a good thing, since we can guarantee Node APIs behaving completely consistent with the browser APIs.

Building Our First Realtime NodeJS App

Without further ado, the entire code is available on github.

Step 0: The idea

In this tutorial, we will be building a realtime aggregation and analytics app using the NodeJS Appbase library. Since we are going to need some data in the first place, we will use an existing app: Jam with Friends, a realtime piano which you can jam together with friends.

Jam with Friends app

One of the best feature of jam-with-friends is creating rooms and jamming together with your friends. It would be a really interesting exercise to find the most popular rooms.

Step 1: Getting started

We will start by installing the Appbase package - npm install appbasejs.

Next and the only dependency, we will using better-console, a replacement for node's default console with colors and more methods - npm install better-console.

And we are all set!

Step 2: Concepts

How do we get the popular rooms? To answer this question, we will understand how Jam With Friends stores data at a very high level.

Jam with Friends data model

After looking at the data model, the answer is simple: Keys are stored as edges to a room. All we need to do is fetch the keys in a room, and keep listening if new keys are pressed.

Step 3: The code

Display

We will display the data as a table, and refresh it every second.

Data Structure

We will use an array datastructure to store the keys for each room, and keep updating it every time a new key is pressed.

Retrieving the keys

This is a three part process:

  1. We will first retrieve the rooms in the piano app using the edge_added event,
  2. We will fetch the room name using the properties event on the room reference and
  3. We will keep listening to the edge_added event on the room reference for new keys pressed.

Here's the entire working code for doing this:

var logger = require('better-console'); // A library that makes logging very easy  
var Appbase = require('appbasejs'); // Appbase nodejs module

// Setting credentials
Appbase.credentials('piano', 'a659f26b2eabb4985ae104ddcc43155d');

var rooms = [];

//refresh the display every sec
setInterval(function() {  
  logger.clear();
  logger.table(rooms); //show the data as a table
}, 1000);

Appbase.ns('pianoapp').v('piano').on('edge_added', function(error, edgeRef, eSnap) {  
  var roomData = ["", 0];
  rooms.push(roomData);
  if(error) {
    return logger.error(error);
  }
  edgeRef.on('properties', function(error, ref, vSnap) {
    //fetching the name of the room
    roomData[0] = vSnap.properties().name;
  })
  edgeRef.outVertex('keys').on('edge_added', function(error, ref, eSnap) {
    //counting number of keys in the room
    roomData[1] += 1;    
    //refresh the view
  })
});

The End Result

How It Looks

If you play keys using the Jam With Friends app, you should see the table update live!

The entire code is also available on github here.

Author image
Founder at Appbase, read my musings on the db world.