Last active
August 29, 2015 14:04
-
-
Save yeukhon/9a5ef85b091287a48552 to your computer and use it in GitHub Desktop.
Simple example of pub/sub using node.js and redis client
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
// Usage: | |
// node pub.js | |
// then starts typing message (ENTER per message) | |
// A publisher could be invokved by a background job | |
// processor or a user triggered action received by | |
// web app. | |
var redis = require("redis"); | |
var pub = redis.createClient(); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.on('data', function (input) { | |
pub.publish("latest", | |
JSON.stringify( | |
{ | |
"msg": input.trim(), // remove whitespaces and \n | |
"time": new Date().toJSON() | |
} | |
) | |
); | |
}); |
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
var redis = require('redis'); | |
var sub = redis.createClient(); | |
// Listens on "latest" channel | |
// when we receive a message, log it. | |
// In real world, you would use this in a web server application | |
// and sends the message to browser client. | |
// (or do whatever you want here, e.g. writing to log file) | |
sub.subscribe("latest"); | |
sub.on("message", function (channel, message) { | |
console.log("subscriber:", message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also use
monitor
in your redis-cli to monitor redis activity.