How to use Firebase on SoloLearn? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

How to use Firebase on SoloLearn?

1st May 2017, 2:48 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
19 Answers
+ 31
(1) Firebase setup: once you created am account you will be given an access script (accessible via AUTHENTICATION->WEB SETUP in the Firebase console) it looks like this: <script src="https://www.gstatic.com/firebasejs/3.7.5/firebase.js"></script> <script> // Initialize Firebase var config = { apiKey: "yourApiKey", authDomain: "yourAuthDomain.firebaseapp.com", databaseURL: "https://yourUrl.firebaseio.com", projectId: "yourProjectId", storageBucket: "yourStorageBucket.appspot.com", messagingSenderId: "someNumber" }; firebase.initializeApp(config); </script> this configures your access point to the database the last line before the </script> initializes the database with your supplied parameters
1st May 2017, 3:50 PM
Burey
Burey - avatar
+ 22
nice explanation :)@Burey
24th Jul 2017, 6:03 AM
Ant●ny
1st May 2017, 3:34 PM
Burey
Burey - avatar
+ 13
(2) Setting to read/write anonymously: first, to write anonymously to the database you must change somesettongs in the Firebase console and database rules Authentication -> SIGN-IN METHOD click Anonymous (last on the list) and enable Database -> RULES { "rules":{ ".read":true, ".write":true } } this will allow to read and write anonymously without a need to first authenticate with the database
1st May 2017, 3:54 PM
Burey
Burey - avatar
+ 13
(3) How will the data look like: imagine we want to create a messaging service and we want to save messages. we will save the messages in a specific structure: every post will be saved in a branch of a posts tree with unique id for each post (firebase assigns those ids) posts:{ someId1:{ author:"Burey", body:"welcome to Burey-Chat!!!", createTime:1483743673728 }, someId2:{......}, }
1st May 2017, 4:03 PM
Burey
Burey - avatar
+ 11
(4) Lets connect to the database and write/read something: 1) get a reference to posts branch in the database: postsRef = firebase.database().ref("posts"); 2) create a listener that will activate each time data is added to the database (new post added) postsRef.on("value", getMessages, onError); as you can see passed 3 parameters to on() method "value" defines on which type of event getMessages is a function (we will define it right away) onError is also a function. types of event: "value": fires every time posts branch is changed (child added,removed,updated) "child_added": fires only when a new child to posts branch added "child_removed": fires when a child is deleted to write data we simply use push to the postsRef: postsRef.push({ author: "Burey", body: "Test Message", createTime: firebase.database.ServerValue.TIMESTAMP }); the create time is taken from the server, otherwise you can mix up times from different regions so use firebase.database.ServerValue.TIMESTAMP and not your own local time
1st May 2017, 4:18 PM
Burey
Burey - avatar
+ 10
anything specific?
1st May 2017, 3:41 PM
Burey
Burey - avatar
+ 10
(5) Reading data: first lets define the functions to handle the on() event function onError(err){ console.log("error occured:"+err); } each call to on returns a snapshot which is the current data at the firebase server, the snapshot is received by tbe function we use as the second parameter to on we need to iterate on the snapshot and extract the data let's save the data into local posts array function getMessages(snapshot){ posts=[]; snapshot.forEach(function(child){ posts.push({ author:child.val().author, body:child.val().body, createTime:child.val().createTime }); }); // you got all the data inside the posts array, do something with it (refresh the user interface) }
1st May 2017, 4:26 PM
Burey
Burey - avatar
+ 9
go ahead @S Vengat you welcome @Abdur
24th Jul 2017, 5:07 AM
Burey
Burey - avatar
+ 8
I wanna try something using firebase
24th Jul 2017, 3:40 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 7
oh didn't see xD no prob :]
1st May 2017, 4:27 PM
Burey
Burey - avatar
+ 5
@Burey I know, but I can't understand such a complicated code. I just need someone explain
1st May 2017, 3:35 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 5
@Burey reading..
1st May 2017, 3:41 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 5
@Burey how we add data?
1st May 2017, 3:55 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 5
@Burey I got it, let me try...
1st May 2017, 4:03 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 4
@Burey thats enough I think. Thank you so much.
1st May 2017, 4:20 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 4
@burey a great thanks
24th Jul 2017, 3:44 AM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
+ 1
bookmarked
16th Jun 2018, 4:10 PM
Masquerade
Masquerade - avatar
+ 1
how do we actully save data?
21st Jul 2020, 1:00 AM
Sali65
Sali65 - avatar