Skip to content

psubscribe patterns not working correctly? #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
saschaishikawa opened this issue Mar 22, 2016 · 5 comments
Closed

psubscribe patterns not working correctly? #270

saschaishikawa opened this issue Mar 22, 2016 · 5 comments

Comments

@saschaishikawa
Copy link

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);
  });
@klinquist
Copy link
Contributor

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

@saschaishikawa
Copy link
Author

@klinquist, good catch! But alas, the 'message' event isn't even firing. I checked redis and the channel is definitely being published.

@klinquist
Copy link
Contributor

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

:)

@saschaishikawa
Copy link
Author

🤦 That takes care of it. Thank you very much for your help and contribution to ioredis. It's been very useful. :) Closing issue now.

@ymeskini
Copy link

ymeskini commented Jan 5, 2022

if everyone comes after the order has changed: sub.on('pmessage', (pattern, channel, message) => {})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants