Created
March 1, 2016 03:39
-
-
Save felipekm/c18a34699222e4140cab to your computer and use it in GitHub Desktop.
Pub/sub with Redis [Node.js and node_redis]
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
var http = require('http'); | |
var url = require('url'); | |
var redis_lib = require("redis"); | |
http.createServer(function (req, res) { | |
var query = url.parse(req.url, true).query; | |
var redis1 = redis_lib.createClient(); | |
var redis2 = redis_lib.createClient(); | |
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); | |
redis2.on("subscribe", function (channel, count) { | |
redis1.publish("echo", query.message || ""); | |
}); | |
redis2.on("message", function(channel, message) { | |
redis2.unsubscribe(); | |
redis2.end(); | |
redis1.end(); | |
res.end(message); | |
}); | |
redis2.subscribe("echo"); | |
}).listen(80); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment