Firebase file streaming with Delphi.

I'm trying different BaaS alternatives as 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:
The following code shows how to stream the file into JSON format using the TDBXJSONTools.StreamToJSON method:

And to translate it back to a file stream:

The source code of the project can be found on my github repository:
In this repository you will find simple examples using different vendors like, 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.

Remember to explore the source code for more. Enjoy!



  1. Do you use notifications from Firebase in your mobile apps? Struggling to get Kinvey to work and looking for an easier alternative. Any suggestions welcome. Thanks.

    1. Hi Richard,

      I haven't tried yet but it's in my list. Hopefully I'll spend some time this year on it and publish it on my blog.

      Watch this space.


  2. Hello Jordi,

    I saw your example using firebase and json to transfer files through TStream.

    Would it be possible before carrying out the conversion to TStream for Json Array to perform the compaction of TStream using the System.zLib and on the client before opening or saving the file perform unpacking?

    Thank you for sharing your knowledge, I always follow your articles.

    Marcio Eduardo

    1. Hi Marcio,

      I don't see why not. You should be able to compress the TStream and then use the same Json conversion to process the file. I might give it a go if I have the time as this could be a good idea.

      Many thanks for following me!


  3. Hi Jordi,

    How are you doing? My name is Adriano Santos, I'm Embarcadero MVP Brazil and I'm trying to implement a routine to upload files to Google Firebase Storage. I ended up getting to your blog.

    I copied the source code, understood and made the necessary changes to upload files to my user on Google. But it gives 401 error, without authorization. I think I read the entire documentation and I can not figure out where the problem might be.

    In Google Firebase where should I release the access to upload those files over there? Can you tell me.

    Thank you, good job.

    1. Hi Adriano,

      What version of Delphi are you using? Did you place the keys in the correct place in the configuration file?



Post a Comment

Popular Posts