diff --git a/firebase-chat-functions.js b/firebase-chat-functions.js new file mode 100644 index 0000000..65a98bb --- /dev/null +++ b/firebase-chat-functions.js @@ -0,0 +1,29 @@ +function saveMessageToFirebase(msg) { + db.collection("messages").add(msg) +} + +function getAllMessagesFromFirebase() { + let allMsgs = []; + db.collection("messages").orderBy('dateStamp', 'asc').limit(200).get().then(function(querySnapshot) { + querySnapshot.forEach(function(doc) { + // doc.data() is never undefined for query doc snapshots + allMsgs.push(doc.data()) + }); + }); + + return allMsgs; +} + +function watchFirebaseForChanges(callBack) { + db.collection("messages").orderBy('dateStamp','asc').onSnapshot(function(querySnapshot) { + querySnapshot.docChanges().forEach(function(change) { + if (change.type === "added") { + callBack(change.doc); + } + }); + }); +} + + +//getAllMessagesFromFirebase(); +watchFirebaseForChanges(function(msg){displayNewMessage(msg.data())}) \ No newline at end of file diff --git a/firebase-config.js b/firebase-config.js new file mode 100644 index 0000000..ea14716 --- /dev/null +++ b/firebase-config.js @@ -0,0 +1,14 @@ + var firebaseConfig = { + apiKey: "AIzaSyAbh39Rb-F9_PO8lpAoob_qA1dYUVNYAkg", + authDomain: "dmd-3440-pwa-demo.firebaseapp.com", + databaseURL: "https://dmd-3440-pwa-demo.firebaseio.com", + projectId: "dmd-3440-pwa-demo", + storageBucket: "dmd-3440-pwa-demo.appspot.com", + messagingSenderId: "239477441112", + appId: "1:239477441112:web:0dcd3f94fe067b09ac402d" + }; + // Initialize Firebase + firebase.initializeApp(firebaseConfig); + var db = firebase.firestore(); + + \ No newline at end of file diff --git a/index.html b/index.html index 7fdbe38..d578682 100644 --- a/index.html +++ b/index.html @@ -10,12 +10,30 @@
-