Skip to content

Instantly share code, notes, and snippets.

@gordienoye
Created March 25, 2014 20:29
Show Gist options
  • Save gordienoye/9770648 to your computer and use it in GitHub Desktop.
Save gordienoye/9770648 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Monad Example</title>
<script src="https://cdn.goinstant.net/v1/platform.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<script>
function dumpTheMessages(messages) {
for (messageId in messages) {
var message = messages[messageId];
var displayElement = '<p>[' + message.timestamp + '] ' +
'<span style="color:red">' + message.user + ' says: </span>' +
message.text + '</p>';
$('body').append(displayElement);
}
}
$(document).ready(function () {
// TODO: replace this URL with your GoInstant application URL
var url = 'https://goinstant.net/<ACCOUNT>/<APPLICATION>';
goinstant.connect(url, function (err, connection, lobby) {
if (err) {
throw "Could not connect to GoInstant: " + err.toString();
}
// get all of the existing messages
var chatMessagesKey = lobby.key('chatMessages');
chatMessagesKey.get(function(err, messages) {
if (err) {
throw "Could not get messages: " + err.toString();
}
dumpTheMessages(messages);
// add in a new message
var newMessage = {
text: 'A new message huzzah',
user: 'Monad Example',
timestamp: Date.now()
};
chatMessagesKey.add(newMessage, function(err, results) {
if (err) {
throw "Could not add new message: " + err.toString();
}
// dividing line to see the difference
$('body').append('<hr>');
// get the messages again
chatMessagesKey.get(function(err, messages) {
if (err) {
throw "Could not get messages: " + err.toString();
}
dumpTheMessages(messages);
});
});
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment