-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
32 lines (29 loc) · 1.01 KB
/
Copy pathscript.js
File metadata and controls
32 lines (29 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const firebaseConfig = {
apiKey: "AIzaSyCSVtkMotInxYNE-fnvfVdaC5aVGlhzj4k",
authDomain: "realtime-chat-57551.firebaseapp.com",
projectId: "realtime-chat-57551",
storageBucket: "realtime-chat-57551.appspot.com",
messagingSenderId: "937988465507",
appId: "1:937988465507:web:ccf97f8919226364f8a956",
};
firebase.initializeApp(firebaseConfig);
const db = firebase.database();
const username = prompt("What's your name?");
document.getElementById("send-message").addEventListener("submit", postChat);
function postChat(e) {
e.preventDefault();
const timestamp = Date.now();
const chatTxt = document.getElementById("chat-txt");
const message = chatTxt.value;
chatTxt.value = "";
db.ref("messages/" + timestamp).set({
usr: username,
msg: message,
});
}
const fetchChat = db.ref("messages/");
fetchChat.on("child_added", function (snapshot) {
const messages = snapshot.val();
const msg = "<li>" + messages.usr + " : " + messages.msg + "</li>";
document.getElementById("messages").innerHTML += msg;
});