Skip to content

Instantly share code, notes, and snippets.

@felipekm
Created March 1, 2016 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipekm/c18a34699222e4140cab to your computer and use it in GitHub Desktop.
Save felipekm/c18a34699222e4140cab to your computer and use it in GitHub Desktop.
Pub/sub with Redis [Node.js and node_redis]
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