Skip to content

Instantly share code, notes, and snippets.

@bwsewell
Last active April 5, 2017 16:56
Show Gist options
  • Save bwsewell/05fd8dd77f39c99650c24d8630040b52 to your computer and use it in GitHub Desktop.
Save bwsewell/05fd8dd77f39c99650c24d8630040b52 to your computer and use it in GitHub Desktop.
Intro to Firebase Realtime Database
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
};
firebase.initializeApp(config);
var database = firebase.database();
var notesRef = database.ref('notes');
var textarea = document.getElementById('notes');
// Save the contents of the textarea to firebase
textarea.addEventListener('keyup', function() {
notesRef.set(textarea.value);
});
// Set of textarea to what we have in firebase any time
// the data on firebase changes (this includes on page load)
notesRef.on('value', function(snapshot) {
textarea.value = snapshot.val();
});
<!DOCTYPE html>
<html>
<head>
<script src="https://www.gstatic.com/firebasejs/3.7.4/firebase.js"></script>
</head>
<body>
<h1>Notes</h1>
<textarea id="notes" cols="100" rows="10"></textarea>
</body>
<script src="app.js"></script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment