I'm trying to publish status messages for each process that gets forked within a script. Each channel is named as status_ followed by some unique identifier, job_id. So I was hoping to use psubscribe to subscribe to all channels matching the pattern status_*, but I'm having trouble doing so. It seems to work OK if I replace the call to redis.psubscribe with a regular redis.subscribe and hard-code the correct channel name, but that defeats the purpose.
redis.psubscribe('status_*', function(error, count){})
redis.on('message', function (channel, message) {
console.log('Received message \'%s\' from channel \'%s\'', message, channel);
});
Then, on another script, I publish a message to the channel
var channel = 'status_' + job_id;
redis.subscribe(channel, function(error, count){
pub.publish(channel, someMessage);
});
When you psubscribe, the messages are returned with three patterns - channel, pattern, and message. Try this:
redis.psubscribe('status_*', function(error, count){})
redis.on('message', function (channel, pattern, message) {
console.log('Received message \'%s\' from channel \'%s\'', message, channel);
});
@klinquist, good catch! But alas, the 'message' event isn't even firing. I checked redis and the channel is definitely being published.
@saschaishikawa Oh yes, one more thing... when you psubscribe, you need to watch for pmessage instead of message.
redis.on('pmessage', function(channel, pattern, message) {});
:)
:facepalm: That takes care of it. Thank you very much for your help and contribution to ioredis. It's been very useful. :) Closing issue now.
Most helpful comment
@saschaishikawa Oh yes, one more thing... when you psubscribe, you need to watch for pmessage instead of message.
redis.on('pmessage', function(channel, pattern, message) {});
:)