Using node js with redis as a cache and pub-sub code. The cache stores list of useful messages. After a point, the messages are deleted from redis. If the number of messages falls below 10, we have to 'publish' a response to all the subscribers to consume and perform some action.
Here is my draft publisher code:
var redis = require('redis')
var pub = redis.createClient(6379, 'localhost');
pub.keys('message:*', function (err, keys){
if (err) return console.log(err);
if (keys.length < 10){
pub.publish("mchannel", "Messages threshold reached", function(err){
if (err) throw err;
});
}
});
Subscriber code:
var redis = require('redis')
var sub = redis.createClient();
sub.on("message", function(channel, message){
console.log("Message '" + message + "' on channel '" + channel + "' received!")
// Some action...
});
sub.subscribe("mchannel");
When i execute the publisher and subscriber code using the node command, the message is published only once..as expected.
How can i constantly poll for cache changes from the publisher code and publish that message?