Ioredis: psubscribe patterns not working correctly?

Created on 22 Mar 2016  路  4Comments  路  Source: luin/ioredis

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);
  });

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) {});

:)

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jamesdixon picture jamesdixon  路  4Comments

PhillipWinder picture PhillipWinder  路  3Comments

snig-b picture snig-b  路  5Comments

kimmobrunfeldt picture kimmobrunfeldt  路  5Comments

lakamsani picture lakamsani  路  3Comments