Created
November 21, 2013 19:38
-
-
Save jparreira/7588147 to your computer and use it in GitHub Desktop.
Listing last n chat messages and retrieve the previous messages on demand.
Messages will be written at the browser console in groups of 3 (last ones first).
Click on the "previous messages" link to retrieve the previous messages until there's no more messages in the table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="ISO-8859-1"> | |
<title>Insert title here</title> | |
<script type="text/javascript" src="http://storage-cdn.realtime.co/storage/1.0.0/realtime-storage-min.js"></script> | |
</head> | |
<body> | |
<a href="javascript:void;" title="previous messages" id="aPreviousMessages">previous messages</a> | |
<script type="text/javascript" src="list.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This example assumes you have a table named chatMessages with the following key-schema: | |
// primary key -> chatid (string) | |
// secondary key -> timestamp (number) | |
// | |
// It should also contain an attribute named text (string) | |
var credentials = { | |
applicationKey: "2Ze1dz", // change this to your application key | |
authenticationToken: "userToken" // change this to your authenticated token if security is being used | |
}; | |
var storage = Realtime.Storage.create(credentials); | |
// we'll use Operation to get the other previous messages | |
// The Operation object gives you access to the underlying REST services | |
var operation = new Realtime.Storage.Operation(credentials); | |
// value of the tables primary key | |
var chatRoom = "test-list"; | |
// all messages will be stored here | |
var messages = []; | |
// number of messages to get (limit) | |
var count = 3; | |
// get the last "count" messages in chatRoom | |
table = storage.table("chatMessages"); | |
table.limit(count).desc().equals({ item: "chatid", value: chatRoom }).getItems(function(itemSnapshot) { | |
if(itemSnapshot) { | |
// unshift will insert the messages in the reverse order | |
messages.unshift(itemSnapshot.val()); | |
} | |
else { | |
// Writes each message at the console in the right order | |
for(var i = 0; i < messages.length; ++i) { | |
console.log(messages[i].text); | |
} | |
var link = document.getElementById("aPreviousMessages"); | |
// user wants the previous messages | |
link.onclick = function() { | |
// get the other previous messages starting from the oldest message shown | |
// we'll use the startKey parameter to tell Cloud Storage the starting point | |
operation.queryItems({ | |
table: "chatMessages", | |
key: { primary: chatRoom }, | |
// the key from where the search should resume | |
startKey: messages.length != 0 ? { | |
primary: messages[0].chatid, | |
secondary: messages[0].timestamp | |
} : undefined, | |
limit: count, | |
searchForward: false | |
}, function(data) { | |
var lastMessages = []; | |
// unshift will insert the messages in the reverse order | |
for(var i = 0; i < data.items.length; ++i) { | |
lastMessages.unshift(data.items[i]); | |
} | |
// display messages in the correct order | |
for(var i = 0; i < lastMessages.length; ++i) { | |
console.log(lastMessages[i].text); | |
} | |
// add messages to the global array containing all displayed messages so far | |
messages = lastMessages.concat(messages); | |
// if there's no stopKey this means there are no more messages to retrieve | |
if(!data.stopKey) { | |
link.innerHTML = "No more messages"; | |
} | |
}, function(err) { | |
console.log(err); | |
}); | |
return false; | |
}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment