Skip to content

Instantly share code, notes, and snippets.

@hieptl
Created September 12, 2022 18:26
Show Gist options
  • Save hieptl/8d26ee7e61da224dcbc1f9bd9890adf3 to your computer and use it in GitHub Desktop.
Save hieptl/8d26ee7e61da224dcbc1f9bd9890adf3 to your computer and use it in GitHub Desktop.
Chat App with Firebase - index.js - 2
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