Sunday, 27 November 2016

I'm trying different BaaS alternatives as Parse.com is shutting down by the end of the year. Luckily for us, we have a really cool alternative: Firebase. Firebase is a unified app platform for Android, iOS and mobile web development. It features RealTime Database, user authenticator and hosting to make application development easier. It provides web app and mobile app developers with a way to link their applications to back-end cloud storage.

In this article I will show you how I interact with the Firebase RealTime Database via the REST API to stream files in JSON format using the latest Delphi 10.1 Berlin update 2.

The following example is just a POC and it will allow you to upload a pdf file via the desktop application, marshall its content into JSON format and then send it via POST to the Firebase Database URL Rest end-point. Then the files are accessed with the (Android) mobile version of it where files are unmarshalled via HTTP Response into JSON format and then transformed back into pdf format. The following diagram explains the process in detail:

In this repository you will find simple examples using different vendors like Parse.com, Kinvey and Firebase, exploring different alternatives using HTTP Rest. You can read my previous articles about this topic here:

Firebase will store your documents in the Firebase Database in JSON format:

To use the Firebase Database URL as an end-point you just need to append .json to the end of the URL and send a request from our HTTPS component. For this example, I've created a project in Firebase called delphitestproject. Each request needs to be authenticated and the easiest way to achieve that is by attaching the apiKey from Firebase to your request. There are other options to authenticate your requests but these require exploring in deep firebase service accounts.

To get the apiKey, go to Settings -> Project Settings -> General, and you will see there the web API Key:

You can test your end-point using curl to simulate the requests:

The process to upload a file works as follows:

And the same solution running on my android and then opening the file from there as it has been downloaded from the cloud:

The Delphi app uses the the latest PPL (Parallel programming Library) to avoid freezing the UI. There are separate TTask that are called for the GET/POST request so there is no call in the UI Thread.
When opening files on the Android app, I had to use Android intents to call the correct pdf component. The solution is still incomplete as I need to find a good way to browse files on my Android device and upload this one in the cloud database. From here onward, the sky is the limit. You could streamline the process by storing file Ids so then the whole content is not downloaded every time (which increases bandwidth usage) and encrypt your pdf files prior to streaming them to the cloud for an additional layer of security.

About the Author

I am a full stack Software Architect and I consider myself a problem solver with the ability of getting things to work. Having a keen eye on quality, architecture and risks this lets me build good software. I am mainly interested in Delphi, .NET, Databases, AI, compilers, grammars, graphics and more mathematical stuff. If you like this page you could also visit me on twitter @thunderjordi and on Facebook.