Write a JS program that records and displays messages. The user can post a message, supplying a name and content and retrieve all currently recorded messages. Use the following HTML to test your code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Messenger</title>
<style>
label { display: inline-block; width: 5em; }
#author, #content { width: 30em; }
</style>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<div id="main">
<textarea id="messages" cols="80" rows="12" disabled="true"></textarea>
<div id="controls">
<label for="author">Name: </label><input id="author" type="text"><br>
<label for="content">Message: </label><input id="content" type="text">
<input id="submit" type="button" value="Send">
<input id="refresh" type="button" value="Refresh">
</div>
</div>
<script src="solution.js"></script>
<script>
attachEvents();
</script>
</body>
</html>
You will need to create the database yourself – use Firebase and set the access rules to be public, so that anyone can post a message and read what’s been posted. Since Firebase objects are by default sorted alphabetically, you’ll need to keep a timestamp property so they can be ordered by most recently posted instead. Use the following message structure:
{
author: authorName,
content: msgText,
timestamp: time
}
The key associated with each message object is not important – when making a POST request with the message object as parameter, Firebase will automatically assign a random key. To get started, you can create a “messenger” entry in your Firebase and import the following JSON object:
{
"-KWi2_-QHxL1yov93j5i" : {
"author" : "Pesho",
"content" : "hi guys",
"timestamp" : 1479315195400
},
"-KWi2aENk0utP8BLnhi6" : {
"author" : "Gosho",
"content" : "whats up",
"timestamp" : 1479315200447
},
"-KWi3eFIUZbh8Z3OjZEB" : {
"author" : "Pesho",
"content" : "not much, how about you?",
"timestamp" : 1479315479039
},
"-KWiX5ixY39AqdD2hJzV" : {
"author" : "LJ",
"content" : "LEEEEROOOY JEEEEENKIIINS",
"timestamp" : 1479323197569
}
}
Examples:
