Firebase C# library

Being a Windows 10/Windows Phone developer is not easy these days. Your user base is substantially smaller compared to Android/iOS which means less companies invest into a dedicated Windows app. This isn’t true just for typical consumer-oriented companies (think Starbucks), but also for technological companies which produce some form of SDKs/libraries/toolkits/you name it. One such problematic example is Firebase, once a startup, now part of Google.

Firebase is a set of tools for developers. One of those tools is a Firebase Realtime Database which handles both offline and online storage of your data as well as the synchronization between the two. From your code you basically call a single method “Save” and Firebase asynchronously takes care of local persistence, sync and streaming of changes coming from other sources (other users/devices etc). It significantly reduces the amount of code you need to write and allows you to focus on your business logic. This is one of the reasons why in Step Up Labs, a startup I recently became part of, a decision was made to completely migrate the backend of our main app — Settle Up — to Firebase.

Sounds great. Until you realize Firebase doesn’t have a C# SDK. What’s worse, they have been saying it’s on their roadmap for three years, after which they released a C++ version first. Then you think you could write the library yourself, since Firebase exposes a set of REST APIs. Unfortunately, after a quick scan it is clear it’s not possible to create a C# library which would also have offline functionality, because the APIs don’t have a way of saying “send me all changes which happened since…”. How do the official SDKs for iOS/Android do it? They use non-public WebSocket APIs, apparently too unstable to make public during the last 3 years.

Fortunately, our database structure contains a collection of all changes (inserts/updates/deletes) which grows each time a change happens. Since the keys should be only incremental I can get new changes since the last sync and query the respective items with given keys. This is not ideal because it is app-specific and will require me to write a lot of code to handle the synchronization myself, but better than nothing.

Alright, enough whining, time for action!

Existing C# REST libraries

Surely I’m not the first C# developer to have the need to access Firebase. There are, as a matter of fact, two existing libraries:

They both have their imperfections, specifically related to realtime streaming. For example, when I subscribe to a location, and a change happens to some nested node, FireSharp won’t correctly pair it with the top level entity and will only return the path and data as a string. I want the deserialization to be automatic. FirebaseSharp‘s streaming implementation is even more unfortunate — at first, it fetches everything from a given location (even when I specify filtering options) and does the filtering locally. This is obviously not usable when there are thousands of items.

Also, neither of them targets Universal Windows Platform.

Introducing FirebaseDatabase.net

That is why I decided to write yet another C# library. And since we looove open source at Step Up Labs we decided to put it publicly on GitHub.

Its use is fairly simple. Here is a sample of a one time query:

var firebase = new FirebaseClient(“https://dinosaur-facts.firebaseio.com/"); 
var dinos = await firebase .Child(“dinosaurs”) 
.OrderByKey()
.StartAt(“pterodactyl”)
.LimitToFirst(2)
.OnceAsync<Dinosaur>();
foreach (var dino in dinos) 
{
Console.WriteLine($”{dino.Key} is {dino.Object.Height}m high.”); }
var firebase = new FirebaseClient(“https://dinosaur-facts.firebaseio.com/");
var dinos = await firebase
.Child(“dinosaurs”)
.OrderByKey()
.StartAt(“pterodactyl”)
.LimitToFirst(2)
.OnceAsync<Dinosaur>();
foreach (var dino in dinos)
{
Console.WriteLine($”{dino.Key} is {dino.Object.Height}m high.”);
}

Btw the names of properties of your classes don’t need to match exactly. You can use the JsonProperty attribute (from json.net) to setup the mappings you want:

public class Dinosaur
{
[JsonProperty(Name = “height”)]
public double H { get; set; }
}

To take advantage of the streaming API, use the AsObservable method which lets you subscribe to realtime changes coming in from Firebase.

var firebase = new FirebaseClient(“https://dinosaur-facts.firebaseio.com/");
var observable = firebase
.Child(“dinosaurs”)
.AsObservable< Dinosaur >()
.Subscribe(d => Console.WriteLine(d.Key));

The cool thing is that this is strongly typed. Even when some nested element changes, this library can correctly associate it with its top level parent and send it to you to handle (for example when a Dinosaur’s height changes you will receive the entire Dinosaur object).

The streaming implementation is also able to recover from loss of connectivity, so you don’t need to worry about that. For more samples, check out our GitHub repo. I would also like to point out that the AsObservable() method returns an IObservable<T> which you can use along with Reactive Extensions.

Where can I get the library?

// install via NuGet
Install-Package FirebaseDatabase.net -pre

Following frameworks are supported:

  • .NET 4.5+
  • Windows 8.x
  • UWP
  • Windows Phone 8.1
  • CoreCLR

What’s next?

I do have some ideas for future development and improvements, but if you have any ideas of your own, please let me know (or even better yet raise an issue on GitHub).

Later, I would like to look at the following (possibly releasing them as separate libraries):

  • File upload
  • Authentication
  • Some form of semi-generic offline storage

Other ideas and/or pull requests are very welcome!