Created
January 27, 2012 16:13
-
-
Save thinkjson/1689549 to your computer and use it in GitHub Desktop.
Real time analytics in Redis and Node.js
This file contains hidden or 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
| { | |
| "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" | |
| } |
This file contains hidden or 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
| // 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