Created
July 20, 2012 17:55
-
-
Save mnrtks/3152255 to your computer and use it in GitHub Desktop.
Redis sample for subscribing multi channels.
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
/* | |
* Redis sample for publishing message. | |
* To send a message to the random to the specified channel. | |
* Usage: node pub.js channel1 channel2 channel3 ... | |
*/ | |
var redis = require('redis'); | |
function main(argv) { | |
var seq = 0; | |
var client = redis.createClient(6379, 'localhost'); | |
setInterval(function () { | |
var channel = argv[parseInt(Math.random() * argv.length)]; | |
var num = client.publish(channel, seq++); | |
console.log("Publish to channel '" + channel + "'", num); | |
}, 1000); | |
} | |
var channels = process.argv.slice(2); | |
main(channels); |
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
/* | |
* Redis sample for subscribing multi channels. | |
* Usage: node sub.js channel1 channel2 channel3 ... | |
*/ | |
var redis = require('redis'); | |
function main(argv) { | |
var client = redis.createClient(6379, 'localhost'); | |
// ready to subscribe | |
client.on('subscribe', function (channel, count) { | |
console.log("Subscribe channel", channel, count); | |
}); | |
// recive message | |
client.on('message', function (channel, count) { | |
console.log("recieved", channel, count); | |
}); | |
// subscribe to start in the specified channel | |
client.subscribe.apply(client, argv); | |
// following is same but three events emit | |
/* | |
for (var i = 0; i < argv.length; i++) { | |
client.subscribe(argv[i]); | |
} | |
*/ | |
} | |
var channels = process.argv.slice(2); | |
main(channels); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment