So I couldn't find a wrapper library that made dealing with chunked encoding over javascript nice. So I made this. It helps you do long polling in javascript using chunked encoding.
This will add messages to the div with that class every time a chunk is received:
<html>
<head>
<meta charset="utf-8">
</head>
<body style="overflow: hidden;">
<h1>Messages:</h1>
<div class="messages"></div>
<script src="js/subscriber.js" type="text/javascript"></script>
<script src="js/jquery-2.1.1.js" type="text/javascript"></script>
<script type="text/javascript">
var s = new Chunky("http://localhost/chunked-encoding-test");
s.on("chunk", function(chunk) {
$(".messages").append($("<p>").text(chunk));
});
</script>
</body>
</html>
This code demonstrates how you would subscribe to a redis channel using webdis :
var s = new Chunky("http://localhost:7379/SUBSCRIBE/test", {
preprocessor: function(chunk) {
// chunk looks like: {"SUBSCRIBE":["message","test","stuff is happening"]}
return JSON.parse(chunk).SUBSCRIBE[2]; // parse the webdis JSON output
}
});
s.on("chunk", function(chunk) {
$(".messages").append($("<p>").text(chunk)); // outputs only the message
});
It probably wouldn't be hard to wrap this with another (WebdisSubscriber) library to make subscribing easier.