-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.js
34 lines (31 loc) · 810 Bytes
/
script.js
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
33
34
// Initialize Firebase
var config = {
apiKey: "AIzaSyC9VRDOmhrvRGc4FZvqzXMStahAb7wdyc8",
databaseURL: "https://hola-chatty.firebaseio.com"
};
firebase.initializeApp(config);
var messagesRef = firebase.database().ref('/messages');
/**
* Sends a message to the Firebase message store.
*
* @param user The name of the user sending the message (string)
* @param message The content of the message (string)
*/
function sendMessage(user, message) {
messagesRef.push({
user: user,
message: message
});
}
/**
* Retrieves messages from Firebase.
*
* @param callback Callback function which takes a username and message
* argument
*/
function getMessages(callback) {
messagesRef.on('child_added', function(data) {
var val = data.val();
callback(val.user, val.message);
});
}