Skip to content

Instantly share code, notes, and snippets.

@jbristowe
Last active June 29, 2017 21:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbristowe/c89a7bcae7fc9a035ee7 to your computer and use it in GitHub Desktop.
Save jbristowe/c89a7bcae7fc9a035ee7 to your computer and use it in GitHub Desktop.

A Very Rough Guide to Firebase and NativeScript

Create Plugin

  • mkdir nativescript-firebase
  • cd nativescript-firebase
  • npm init
  • tns init
  • mkdir -p platforms/ios
  • touch platforms/ios/Podfile
  • In platforms/ios/Podfile: pod 'Firebase'
  • mkdir -p platforms/android/libs
  • Copy the Firebase SDK JAR to platforms/android/libs
  • (Optional) touch platforms/android/AndroidManifest.xml
  • (Optional) In platforms/android/AndroidManifest.xml:
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-permission android:name="android.permission.INTERNET" />
</manifest>

Import Plugin

  • In your NativeScript project: tns plugin add <location of nativescript-firebase>

Use Plugin on Android

var appModule = require("application");

// ...

var Firebase = com.firebase.client.Firebase;
var ChildEventListener = com.firebase.client.ChildEventListener;

Firebase.setAndroidContext(appModule.android.context);
ref = new Firebase(<Firebase URL>);

ref.addChildEventListener(new ChildEventListener({
	onChildRemoved: function(snapshot) {
		// do your thing
	},
	onChildAdded: function(snapshot, previousChildKey) {
		// do your thing
	},
	onChildChanged: function(snapshot, string) {
		// do your thing
	},
	onChildMoved: function(snapshot, string) {
		// do your thing
	}
}));

To add new items:

var data = new java.util.HashMap();
data.put(...);
ref.push().setValue(data);

To delete items:

ref.child(id).setValue(null);

And so on.

Use Plugin on iOS

var ref = new Firebase(<Firebase URL>);

ref.observeEventTypeWithBlock(FEventType.FEventTypeChildAdded, function (snapshot) {
	// do your thing
});

ref.observeEventTypeWithBlock(FEventType.FEventTypeChildRemoved, function (snapshot) {
	// do your thing
});

ref.observeEventTypeWithBlock(FEventType.FEventTypeChildChanged, function (snapshot) {
	// do your thing
});

To add new items:

ref.childByAutoId().setValue(...);

To delete items:

ref.childByAppendingPath(id).setValue(null);

And so on.

@x4080
Copy link

x4080 commented Nov 24, 2015

Hi John, did you try the offline firebase capabilities ? In Android seems work but not in IOS

@EddyVerbruggen
Copy link

Hey guys, just wanted to share I'm working on a Firebase plugin for NativeScript based on this Gist. I'm also including the cool recursive JSON to/from Java HashMap methods @afortaleza provided. And for folks interested how iOS deals with this: from JSON to iOS is just fine out of the box, but the way back it's not. You'll get a structure like this:

  user =     {
      address =         {
          number = 123;
          street = foostreet;
      };
      birthYear = 1977;
      first = Eddy;
      isMale = 1;
      last = Verbruggen;
  };

Which can be transformed back to JSON by this function:

firebase.toJsObject = function(objCObj) {
  if (typeof objCObj == "string"||
      typeof objCObj == "number" ||
      typeof objCObj == "boolean") {
    return objCObj;
  }
  var node = {};
  var oKeyArr = objCObj.allKeys;
  for (var i = 0, l = oKeyArr.count; i < l; i++) {
    var key = oKeyArr.objectAtIndex(i);
    var val = objCObj.valueForKey(key);

    switch (types.getClass(val)) {
      case 'NSMutableDictionary':
        node[key] = firebase.toJsObject(val);
        break;
      case 'String':
        node[key] = String(val);
        break;
      case 'Boolean':
        node[key] = Boolean(String(val));
        break;
      case 'Number':
        node[key] = Number(String(val));
        break;
    }
  }
  return node;
};

And can be called like this from one of your observeEventTypeWithBlock methods:

  var value = firebase.toJsObject(snapshot.value)

I'll post a link to the plugin here once it's done.

@EddyVerbruggen
Copy link

It's ready to go, I hope y'all like it: https://github.com/EddyVerbruggen/nativescript-plugin-firebase

@jbristowe
Copy link
Author

👍

Awesome stuff, Eddy!

@lyonzy
Copy link

lyonzy commented Nov 27, 2015

Great minds think alike! I just created the nativescript-firebase plugin on npm based on the Firebase node.js SDK :)
Hey @EddyVerbruggen, just checking out your source - if you want a simpler way to turn Android objects back into JSON I used this slightly hacky but fairly concise trick back when I was using the Firebase Android SDK in {N}:

            var jobj = new org.json.JSONObject(javaObject);
            return JSON.parse(jobj.toString());

It works for objects and arrays but IIRC you'll need to check for strings/ints/etc and return them directly.
I never got around to doing it the other way around but presumably it's a similar concept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment