Created
September 12, 2022 18:26
-
-
Save hieptl/8d26ee7e61da224dcbc1f9bd9890adf3 to your computer and use it in GitHub Desktop.
Chat App with Firebase - index.js - 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var firebaseConfig = { | |
apiKey: "xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx", | |
authDomain: "xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx", | |
databaseURL: "xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx", | |
projectId: "xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx", | |
storageBucket: "xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx", | |
messagingSenderId: "xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx", | |
appId: "xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx", | |
}; | |
firebase.initializeApp(firebaseConfig); | |
const db = firebase.database(); | |
const username = prompt("Please Tell Us Your Name"); | |
function sendMessage(e) { | |
e.preventDefault(); | |
// get values to be submitted | |
const timestamp = Date.now(); | |
const messageInput = document.getElementById("message-input"); | |
const message = messageInput.value; | |
// clear the input box | |
messageInput.value = ""; | |
//auto scroll to bottom | |
document | |
.getElementById("messages") | |
.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" }); | |
// create db collection and send in the data | |
db.ref("messages/" + timestamp).set({ | |
username, | |
message, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment