Skip to content

Instantly share code, notes, and snippets.

@thinkjson
Created January 27, 2012 16:13
Show Gist options
  • Select an option

  • Save thinkjson/1689549 to your computer and use it in GitHub Desktop.

Select an option

Save thinkjson/1689549 to your computer and use it in GitHub Desktop.
Real time analytics in Redis and Node.js
{
"Jan 27 2012 11:03": "139990",
"Jan 27 2012 11:04": "153905",
"Jan 27 2012 11:05": "153666",
"Jan 27 2012 11:06": "150723",
"Jan 27 2012 11:07": "145660",
"Jan 27 2012 11:08": "85044"
}
// Import libraries
var redis = require("redis"),
client = redis.createClient();
var express = require('express');
var app = express.createServer();
// Expose an endpoint which reports on the latest has values out of Redis
// ~2ms response time
app.get('/', function(req, res){
client.hgetall("node_test", function(err, replies) {
res.send(replies);
});
});
// Error handling
client.on("error", function (err) {
console.log("Error " + err);
});
// Simulate incoming data
// Updates are a single hincrby at O(1) complexity
setInterval(function() {
client.hincrby("node_test", (new Date()).toString().substr(4, 17), Math.floor(Math.random() * 1000), function() {});
}, 200);
// Start the server
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment