Alex Kyriakidis
Author - Educator - Consultant
Firebase allows you to store, sync and manage your data.
It provides a nice API that easily integrates into your project.
⏱ Real Time Database
🔑 Authentication
⛅️ Cloud Functions
🌍 Hosting
🗄 Storage
...
The Firebase Realtime Database is a cloud-hosted database.
Data is stored as JSON and synchronized in realtime to every connected client.
// get an id for the new entry
const userId = firebase.database().child('users').push().key
// set entry
firebase.database().ref('users/' + userId).set({
username: 'hootlex',
email: 'alex@vueschool.io',
profile_picture : '...'
});
// listen to 'child_added'
var messagesRef = firebase.database().ref('messages')
messagesRef.on('child_added', (snapshot) => {
this.messages.push(snapshot.val())
});
<div id="app">
<h1>Firebase Real Time DB</h1>
<div v-for="message in messages">
{{ message.text }}
</div>
<br>
<textarea v-model="newMessage"></textarea>
<button @click="send">Send</button>
</div>
new Vue({
el: '#app',
data: {
messages: [],
newMessage: null
},
methods: {
send () {
// Get a key for a new Message.
var newMsgKey = firebase.database().ref().child('messages').push().key
firebase.database().ref().child(`messages/${newMsgKey}`).set({
text: this.newMessage
})
// reset new message text
this.newMessage = null
}
},
mounted () {
var config = {
apiKey: "AIzaSyCDbzBcYN2fbLo6Bn1CT0ohjbPhnxh7rIc",
authDomain: "skg-chat.firebaseapp.com",
databaseURL: "https://skg-chat.firebaseio.com",
projectId: "skg-chat",
storageBucket: "skg-chat.appspot.com",
messagingSenderId: "198257086299"
};
firebase.initializeApp(config);
var messagesRef = firebase.database().ref('messages')
messagesRef.on('child_added', (snapshot) => {
this.messages.push(snapshot.val())
});
}
})
JSFiddle 🐠
Heavy relational models
VueSchool.io
By Alex Kyriakidis