nik-zp / Vue-Mqtt

Connect to mqtt through websocket, implementation for Vuejs 2
95 stars 33 forks source link

How to handle + and # topics? #1

Closed pbpraha closed 7 years ago

pbpraha commented 7 years ago

Hi :)

Thanks for a great library :) Unfortunately, I'm unable to bind to + and # topics, subscribing works though.

this.$mqtt.subscribe('param/param/param/#') <- will subscribe

but...

mqtt: { 'param/param/param/#': function(val) { console.log('this method ...') }, },

does not log :(

i also tried : "param/param/param/" "param/param/param"

etc.

Can you advise please?

Thanks.

cuanjooste commented 7 years ago

I have the same issue as above.

pbpraha commented 7 years ago

As a workaround (or is it the proper way?) I did this in my component mounted() method:

this.$mqtt.subscribe('param/param/param/#')

this.$mqtt.on('message', (topic, message) => { if (topic.match('param/param/param/')) { console.log('matched!') } })

Hope it helps :)

cuanjooste commented 7 years ago

Thank you so much for this. Even though this is potentially a workaround, it does seem to add some additional flexibility. Thanks again!

nik-zp commented 7 years ago

Hi. I use so:

created () {
  this.$mqtt.subscribe('WebExtension/v1/' + this.userProfile.email + '/#')
}
mqtt: {
  Payment_Rsp (response) {
    ...
  },
  BuyResult_Ind (response) {
    ...
  }
}

Full topic is "WebExtension/v1/none@none.com/Payment_Rsp" and "WebExtension/v1/none@none.com/BuyResult_Ind"

pbpraha commented 7 years ago

Hi,

Maybe i'm mis-using / misunderstanding mqtt topics.

I'm using topic === 'provisioning/key/confirm/123456' where '123456' is actually a variable.

that's why i can't use : mqtt: { [SOME_VARIABLE_NAME] (response) { ... }, ...

The variable name is not known ahead of time.

I'm building a handshaking / provisioning system so the '123456' is a keycode the user enters on a remote keypad and has to be passed around for a little while.

nik-zp commented 7 years ago

I can change to work like this:

created () {
  this.$mqtt.subscribe('provisioning/key/confirm/#')
}
mqtt: {
  allTopics (response) {
    // for all topic, 
    // for example 'provisioning/key/confirm/123' | 'provisioning/key/confirm/456'
  }
}
pbpraha commented 7 years ago

hmm, i have other subs on this component too, they would also match, no?

nik-zp commented 7 years ago

All topics that match ''provisioning/key/confirm/#"

pbpraha commented 7 years ago

hmm, what if this ... ?

created () { this.$mqtt.subscribe('provisioning/key/confirm/#') this.$mqtt.subscribe('provisioning/something/something/#')
} mqtt: { allTopics (response) {...} }

nik-zp commented 7 years ago

oh... I understood the problem. I'll think about it and write.

pbpraha commented 7 years ago

Cheers :)

nik-zp commented 7 years ago

Done. See example https://github.com/nik-zp/Vue-Mqtt-Example

cuanjooste commented 7 years ago

Thank you very much for this. I tested it and everything seems to be working fine. I am not sure if this will be a separate request or maybe just something to figure out in the future, but one advantage that this,$mqtt.on still has is that its possible to see the original topic.

An example to highlight this is Publish to : 'results/1/led' and 'results/2/led' Subscribe to : 'results/+/led/' MQTT function only has a parameter of val, so I am currently unable to distinguish between 1 & 2.

I understand this was not in the original request but just thought I would mention it. Thanks again for the quick work!

nik-zp commented 7 years ago

@cuanjooste, hi. see example for vue-mqtt@2.0.1:

mqtt: {
  'results/+/led' (data, topic) {
    if (topic.split('/')[1] === '1234') {
      // ...
    }
  }
}
cuanjooste commented 7 years ago

@nik-zp That is perfect, thank you so much!!!!

pbpraha commented 7 years ago

Thanks also ! :)

I had to move off this project for now but will be sure to use this when i get back to it.