CedricLaberge
CedricLaberge

Reputation: 662

Get channel name in Redis messages on Node.js

I am using Redis as a Pub/Sub client in a Node.js application. I bind a subscriber to a channel pattern like thus:

client.psubscribe("events.*.new", (event) => console.log("NEW EVENT", event));

Then, I send a message on a specific channel this way:

client.publish("events.123.new", '{"some": "data"}');

In my terminal, I get the following output:

NEW EVENT null

My question is 2-fold:

  1. Any reason why my data might end up being null?
  2. Is there a way to access the full name of the channel over which a message has been sent? Since I am listening on the pattern events.*.new, it does not appear that I can know if a message was sent on channel events.123.new or on events.456.new. Is passing this information within the payload the only way to access it in listeners?

Thanks in advance!

Upvotes: 0

Views: 1158

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

Try this:

client.psubscribe("events.*.new");

client.on('pmessage', function (pattern, channel, message) {
    console.log(pattern, channel, message);
});

client.publish("events.123.new", '{"some": "data"}');

The callback to psubscribe just returns error in the first argument if there is an error. You can find more info here

Upvotes: 1

Related Questions