1
votes

I have mqtt client, from mqtt node module.

I subscribe to topics for exemple topic1/#, topic2/#

When someone publish to topic2/165 (for exemple), I want to know that subscribed topic "topic2/#" matched.

Is there simple way to do that ?

1

1 Answers

1
votes

Use a regular expression

client.on('message', function (topic, message) {
  var topic1_re = /^topic2\/.*/;
  var topic2_re = /^topic2\/.*/;

  if (topic.matches(topic1_re)) {
    //topic 1
  } else if (topic.matches(topic2_re)) {
    //topic 2
  }
}