All Projects → JSneak → firebase-tutorial

JSneak / firebase-tutorial

Licence: MIT license
5 minute Firebase Web Database Tutorial

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to firebase-tutorial

Fcm Toolbox
📲 Firebase Cloud Messaging toolbox
Stars: ✭ 217 (+734.62%)
Mutual labels:  firebase-database
angular-firebase-user-profiles
Basic user profile management built on Angular 1.x and Firebase.
Stars: ✭ 32 (+23.08%)
Mutual labels:  firebase-database
firebase-chat-sample
A sample app that shows basic usage of Firebase Auth and Database in form of a very simple chat hub app
Stars: ✭ 21 (-19.23%)
Mutual labels:  firebase-database
Firebase Esp8266
ESP8266 Firebase RTDB Arduino Library
Stars: ✭ 228 (+776.92%)
Mutual labels:  firebase-database
FirebaseAI-Android-Chat-App
A simple firebase enabled chat app. SMS & Email Password Authentication enabled.
Stars: ✭ 38 (+46.15%)
Mutual labels:  firebase-database
Kv-z
Online Quiz App using Firebase
Stars: ✭ 17 (-34.62%)
Mutual labels:  firebase-database
Firebase Esp32
ESP32 Firebase RTDB Arduino Library
Stars: ✭ 204 (+684.62%)
Mutual labels:  firebase-database
Chatter
Real time chat app written in Swift 4 using Firebase
Stars: ✭ 30 (+15.38%)
Mutual labels:  firebase-database
RxFirebase
A RxJava wrap for Google Firebase.
Stars: ✭ 35 (+34.62%)
Mutual labels:  firebase-database
anonymous-web
💬 A PreactJS powered progressive web (chat) application (Not active)
Stars: ✭ 28 (+7.69%)
Mutual labels:  firebase-database
Salada
Firebase model framework Salada. Salada is the best Firebase framework.
Stars: ✭ 228 (+776.92%)
Mutual labels:  firebase-database
Firebase Ios Sdk
Firebase iOS SDK
Stars: ✭ 3,309 (+12626.92%)
Mutual labels:  firebase-database
Bitcamp-2019
Won the most innovative solution at Bitcamp 2019.🎖🎉
Stars: ✭ 15 (-42.31%)
Mutual labels:  firebase-database
Firebase Authentication Dotnet
C# library for Firebase Authentication
Stars: ✭ 222 (+753.85%)
Mutual labels:  firebase-database
Android-ORM-Benchmarks
No description or website provided.
Stars: ✭ 25 (-3.85%)
Mutual labels:  firebase-database
Firebase Kotlin Sdk
A Kotlin-first SDK for Firebase
Stars: ✭ 214 (+723.08%)
Mutual labels:  firebase-database
uMe
Online Chatting Application (Android) || Messaging App || Firebase
Stars: ✭ 138 (+430.77%)
Mutual labels:  firebase-database
firebase-bundle
A Symfony Bundle for the Firebase PHP Admin SDK
Stars: ✭ 112 (+330.77%)
Mutual labels:  firebase-database
firebase-php
Firebase Realtime Database PHP Wrapper
Stars: ✭ 41 (+57.69%)
Mutual labels:  firebase-database
firebase-swiss
The Firebase Swissknife 🇨🇭
Stars: ✭ 12 (-53.85%)
Mutual labels:  firebase-database

Get Started With Firebase in 5-10 Minutes

Table of Contents

The goal of this article is to expose you to the basics of Firebase and provide simple, working examples. If you want to go more in depth, check out Firebase's documentation


What is Firebase?

Firebase is a platform that allows you to build websites without server side code. It has a ton of cool features but we are going to be focused on it's Realtime database.

Now lets get started.


Adding Firebase to your website.

  • Step 1: Go to console.firebase.google.com
  • Step 2: Add a project
  • Step 3: Press "Add Firebase to your web app"
  • Step 4: Copy that code into your HTML page

Setting up the Realtime Database

  • Step 1: Go into your Database
  • Step 2: Click the rules tab
  • Step 3: Change your rules to the code snippet below. Normally you shouldn't do this due to security reasons, but for testing purposes it will be fine. Read more about it here
{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}
  • Step 4: Download the demo.json from here. Right click and Save as on the page to download it. Don't forget to name is demo.json, not demo.json.txt.
  • Step 5: Import demo.json to firebase

Pulling data from the Realtime Database

First add the following code to your script tag.

var database = firebase.database();

There are two main ways to get data from the database, but for now we are going to focus on .once. Use this if you want to pull from the database just one time.

database.ref('/').once('value', function(snapshot){
  console.log(snapshot.val());
});

The database will listen at the root directory, which is done with .ref('/'). Below is another way to declare the .ref.

var rootRef = database.ref('/');
rootRef.once('value', function(snapshot){
    console.log(snapshot.val());
});

When you make a call to the Firebase Database, it will return a snapshot, which is data at the reference point you gave it. To access this data, we use .val().

Now, lets say that our data was in a different child location, as it is in the image below.

Getting that data is still easy. Instead of database.ref('/'), we would do database.ref('/username') to get the data.

Now, what if you want to continue to listen for updates past the first one? In that case, you would use .on()

What .on() does is that it will listen for a certain event at a specific child. The different types of events are shown below.

We are going to be focusing on 2 of the events, child_added and child_changed events.

child_added event

This will run when the user loads the page, and can be used in place of a .once(). This will return the values that are pushed/set.

pushDataRef = database.ref("/pushData");
pushDataRef.on("child_added", function(snapshot){
  console.log("Below is the data from child_added");
  console.log(snapshot.val());
});

child_changed event

This will return the values that are changed.

setDataRef = database.ref("/setData");
setDataRef.on('child_changed', function(snapshot) {
  console.log("Below is the data from child_changed");
  console.log(snapshot.val());
});

If you aren't sure if you are updating or adding, then just have the Firebase Database on your screen as it will flash different colors based on what event is going on.

Updating data in the Realtime Database

Lets add a button and text box on our page for this example.

<button onClick="pushData()">Add data to Firebase</button>
<input type="value" id="dataValue"></input>

The two ways I update/create values in the database is with .push() and .set().

You use .push() if you want to create a new child location with a new generated id. You can then use .set() to update a key:value pair.

Below is an example of using .push() and .set()

function pushData(){
  var data = document.getElementById("dataValue").value;
  var dataRef = database.ref('/').push();//Generates a new child location with a randomly generated id.
  dataRef.set({
    value: data
  });
}

Another example, except this time it is only using .set.

function setData(){
  var data = document.getElementById("dataValue").value;
  var dataRef = database.ref('/newData');
  console.log(data)
  dataRef.set({
    value: data
  });
}

If you want to make a new parent and child without having to use .push(), you would do this if you don't want to use a firebase generated UID, then do the example below.

function setData(){
  var data = document.getElementById("dataValue").value;
  var dataRef = database.ref('/newData/' + "nameOfNewParent");
  console.log(data)
  dataRef.set({
    value: data
  });
}

Dealing with Firebase Generated Unique Ids

Now, you might have noticed that Firebase creates a crazy string of characters and numbers when you use .push().

This is the UID, unique ID, that Firebase generates when we use .push(). We can get the UID of a child by using either Object.keys() or .key() as demonstrated below.

Object.keys()

This will return an array of keys.

database.ref('/pushData').once('value', function(snapshot){
  console.log(Object.keys(snapshot.val()));
})

.key

This will return the key of 1 object.

database.ref('/pushData').once('value', function(snapshot){
  snapshot.forEach(function(data){
    console.log(data.key);
  });
})

Conclusion

Hope this helps with getting you started with Firebase. If you have any issues or questions, create an issue here.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].