At icapps, we use Parse to build the backend for a couple of our apps. However, the biggest risk of using a BaaS (Backend as a Service) is Vendor Lockin. The fact that you tie the apps that you build directly into one of the big BaaS providers. Which means that they have power over you, especially if you don’t abstract the way your app is tied to the backend.

The BaaS evolution is an interesting one, have a look at the map Kinvey has created:

Kinvey BaaS Ecosystem

You can see that there is a complex web of relationships between the major BaaS providers.

Since the news that Parse has been bought by Facebook, I’ve been investigating an open source alternative for Parse.

I looked at Helios and Deployd, both of which can be deployd to Heroku.

For this blogpost I’ve chosen to show you how to get started with Deployd, and how you can deploy it to Heroku.

For this blogpost you need a Heroku account, and a credit card (which should not be charged.)

Deployd is an open source api-building framework built on top of Node.js and Mongodb and supports socket.io.

First lets have a look at what you need to install and deploy Deployd. Go over to node.js and install it through the installer package.

This will give you the node and npm utilities that you can use on the command line.

In your Developer folder, create a folder to put the app in.

$ mkdir icapps-deployd

Also create a temporary deployd project to make sure it is working locally.

First install deployd, you can do that by downloading the installer on deployd.com http://deployd.com/download.html
check that it is installed correctly with

$ dpd -V

should output something like 0.6.10

Next we can create the application locally:

$ dpd create icapps-deployd-local

and check that it is working correctly

$ cd icapps-deployd-local

you are now in ~/Developer/icapps-deployd/icapps-deployd-local

$ dpd

Point your browser to http://localhost:2403/dashboard. It should show you the deployd dashboard. We could use the local server to build and test the app, and that is what you should do. But now we’ll prepare an application to deploy to Heroku instead. Setting up a deployd app for Heroku takes a bit of a different approach.

In your terminal press ctrl-c twice to quit the dpd app. Let’s create a second application which we call icapps-deployd-demo.

$ cd ..
$ mkdir icapps-deployd-demo
$ cd icapps-deployd-demo

You are now in ~/Developer/icapps-deployd/icapps-deployd-demo, which you can verify using the pwd command (print working directory).

then run

$ npm install deployd

This installs the deployd package and all dependencies locally for the demo project. (there will be a node_packages folder)

Also install mongodb on your machine:

$ brew install mongodb

If you don’t have brew, you can install it, or use macports.

$ sudo npm install -g mongodb

Then we can start building the application itself.
create a file app.js in the root of the project. (~/Developer/icapps-deployd/icapps-deployd-demo)

$ touch app.js

open the file in your favorite editor and write the following code:

var deployd = require('deployd');

var options = {
  port:process.env.PORT || 3000
};

var dpd = deployd(options);
dpd.listen();

Next we need to copy over the resources from the local project we created earlier with dpd

$ cp -R ../icapps-deployd-local .

Running

$ ls

shows you:

app.dpd      app.js       data         node_modules public       resources

We don’t need app.dpd, so

$ rm app.dpd

Next we need to create an access key for the application.

$ dpd keygen
$ dpd showkey

Copy this key to your clipboard, because we will need it shortly.
now, lets startup the application.

$ mongod & node app.js

and point your browser to
http://localhost:3000/dashboard/

Provide the key you have in your clipboard and deployd should work.

Our local preparation for the app is almost finished. We will now define the dependencies for our application for heroku. This way heroku can install the dependencies via npm.

$ touch package.json

And use your favorite editor and write:

{
  "name":"icapps-deployd",
  "version":"0.0.1",
  "dependencies": {
    "deployd":"~0.6.10",
    "mongodb" : "1.1.11"
  },
  "engines": {
    "node": "0.10.x",
    "npm": "1.2.x"
  }
}

Next we’ll also define Procfile, so that Heroku knows how to start the application

$ touch Procfile

And use your favorite editor and write:

web: node app.js

If you don’t have a Heroku account, setup an account on https://id.heroku.com/signup and download and install the Heroku toolbelt via https://toolbelt.herokuapp.com/. An heroku account is free for a single web dyno, which is more than sufficient for the demo.

Back in the command line, create the git repository that we will use to push the code to heroku.

$ git init
$ git add .
$ git commit -m “Deployd on Heroku, initial commit”

Then we create an application on Heroku using

$ heroku create

Heroku will choose a random name for your new app, you can choose your own (unique) name using ‘heroku create my-app-name’

Mine was called secure-fortress-2529.herokuapp.com.

We need to configure a database provider as well. We can do this in the heroku dashboard, by adding an add-on for the app, or we can use the toolbelt.

$ heroku addons:list

gives an overview of the available addons for our app.

We will need mongodb, so lets filter

$ heroku addons:list | grep mongo

There are two options for the moment: mongohq and mongolab. For the demo I’ve selected mongohq. So let’s setup the addon:

$ heroku addons:add mongohq:sandbox

Mongohq:sandbox is free, but you can scale it up if necessary. It is possible that you need to register your creditcard with heroku before you can add the addon. It should not be charged for this demo, but read the fine-print in any case.

Next, we need to configure our deployd application to connect to it. Therefore we need to find the database location and credentials.

Point your browser to your application in the heroku dashboard and find the addon. Clicking it should take you to the dashboard, which shows you the database that was created for you.

Mine was “mongo alex.mongohq.com:10084/app14888533 -u <user> -p<password>”. Yours will be different!

So we need to define a user and password, that the app can use to connect to the database. In Admin (in the menu on the left) tab Users, add a new user and password. I used icapps-app and icapps-pwd, but have since changed the password ;-)

Make sure to remember the username and password. The password is saved after one-way hashing it, so it is not recoverable. (you can reset it though).

Now let’s test if the db is up and running

$ mongo alex.mongohq.com:10084/app14888533 -u icapps-app -p icapps-pwd

Again, make sure you replace this with the address that is genereated for you.

And it is working. Type exit<enter> to quit the MongoDB shell.

Next we need to change the db settings for deployd, so open up app.js and make sure it reads like this:

var deployd = require('deployd');

var options = {
  port:process.env.PORT || 3000,
  db:{
    port:10084,              // 
    host:"alex.mongohq.com", //   
    name:"app14888533",      //   
    credentials:{            //   / replace with your own settings
      username:"icapps-app", //  /
      password:"icapps-pwd"  // /
    }
  }
};

var dpd = deployd(options);
dpd.listen();

Make sure to replace the port, host, name and credentials.username and credentials.password with what was created for you.

We can now commit that change

$ git commit -am “Changed db settings”

Next we push our app to heroku using

$ git push heroku master

If you visit your application (mine was secure-fortress-2529.herokuapp.com), you get an application error. Let’s check why

$ heroku logs

this shows that the directory resources is missing.

app[<a href="http://web.1">web.1</a>]: Error loading resources:
app[<a href="http://web.1">web.1</a>]: Error: ENOENT, readdir 'resources'

This is annoying, because git doesn’t know about directories. The resources directory is empty, so git ignores it. We can easily fix this.

$ touch resources/.gitemptydir
$ git add *
$ git commit -am "make sure resources are there"

Again try to push the app to heroku:

$ git push heroku master

Try and visit the application now on http://secure-fortress-2529.herokuapp.com (your url will be different.)

In the addressbar, add /dashboard, and we are up and running. (Use dpd showkey if you forgot your key)

Refer to the Deployd examples and documentation to build the rest of the website and REST api.

In a next blogpost, I will show you how to use DeploydKit to connect to this backend and build a small application, which also uses Socket.IO for realtime communication. We could also build a web-app using for example Angularjs, that connects to this backend.

The main advantage of not using one of the big BaaS providers is that you can always take your backend code and move it to your own server, or another IaaS provider. Another important advantage is that you can see and change the open source code (contributing back to the open source community) should the need arise.

The disadvantage is that the open source solutions cannot currently compete on features well with the big BaaS providers, and as a consequence, the up-front costs of choosing and maintaining the open source solution will be higher. Freedom comes with a price.