Coder Social home page Coder Social logo

firebase-database-dotnet's Introduction

FirebaseDatabase.net

AppVeyor Build status

Simple wrapper on top of Firebase Realtime Database REST API. Among others it supports streaming API which you can use for realtime notifications.

For Authenticating with Firebase checkout the Firebase Authentication library and related blog post

To upload files to Firebase Storage checkout the Firebase Storage library and related blog post

Installation

// Install release version
Install-Package FirebaseDatabase.net

// Install pre-release version
Install-Package FirebaseDatabase.net -pre

Supported frameworks

.NET Standard 2.0 - see https://github.com/dotnet/standard/blob/master/docs/versions.md for compatibility matrix

Usage

Authentication

The simplest solution where you only use your app secret is as follows:

var auth = "ABCDE"; // your app secret
var firebaseClient = new FirebaseClient(
  "<URL>",
  new FirebaseOptions
  {
    AuthTokenAsyncFactory = () => Task.FromResult(auth) 
  });

Note that using app secret can only be done for server-side scenarios. Otherwise you should use some sort of third-party login.

var firebaseClient = new FirebaseClient(
  "<URL>",
  new FirebaseOptions
  {
    AuthTokenAsyncFactory = () => LoginAsync()
  });

...

public static async Task<string> LoginAsync()
{
  // manage oauth login to Google / Facebook etc.
  // call FirebaseAuthentication.net library to get the Firebase Token
  // return the token
}
  

As you can se, the AuthTokenAsyncFactory is of type Func<Task<string>>. This is to allow refreshing the expired token in streaming scenarios, in which case the func is called to get a fresh token.

Querying

using Firebase.Database;
using Firebase.Database.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.");
}

Saving & deleting data

using Firebase.Database;
using Firebase.Database.Query;
...
var firebase = new FirebaseClient("https://dinosaur-facts.firebaseio.com/");

// add new item to list of data and let the client generate new key for you (done offline)
var dino = await firebase
  .Child("dinosaurs")
  .PostAsync(new Dinosaur());
  
// note that there is another overload for the PostAsync method which delegates the new key generation to the firebase server
  
Console.WriteLine($"Key for the new dinosaur: {dino.Key}");  

// add new item directly to the specified location (this will overwrite whatever data already exists at that location)
await firebase
  .Child("dinosaurs")
  .Child("t-rex")
  .PutAsync(new Dinosaur());

// delete given child node
await firebase
  .Child("dinosaurs")
  .Child("t-rex")
  .DeleteAsync();

Realtime streaming

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

AsObservable<T> methods returns an IObservable<T> which you can take advantage of using Reactive Extensions

firebase-database-dotnet's People

Contributors

bezysoftware avatar bgrabbc avatar briandunnington avatar c17r avatar cabauman avatar davidvavra avatar echernyavsky avatar kodybrown avatar schmitzt3 avatar smrtsmrf avatar yohaas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.