Skip to content
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

How to handle + and # topics? #1

Closed
pbpraha opened this issue Jul 19, 2017 · 16 comments
Closed

How to handle + and # topics? #1

pbpraha opened this issue Jul 19, 2017 · 16 comments

Comments

@pbpraha
Copy link

pbpraha commented Jul 19, 2017

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
Copy link

I have the same issue as above.

@pbpraha
Copy link
Author

pbpraha commented Jul 19, 2017

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
Copy link

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
Copy link
Owner

nik-zp commented Jul 19, 2017

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
Copy link
Author

pbpraha commented Jul 19, 2017

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
Copy link
Owner

nik-zp commented Jul 19, 2017

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
Copy link
Author

pbpraha commented Jul 19, 2017

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

@nik-zp
Copy link
Owner

nik-zp commented Jul 19, 2017

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

@pbpraha
Copy link
Author

pbpraha commented Jul 19, 2017

hmm, what if this ... ?

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

@nik-zp
Copy link
Owner

nik-zp commented Jul 19, 2017

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

@pbpraha
Copy link
Author

pbpraha commented Jul 19, 2017

Cheers :)

@nik-zp
Copy link
Owner

nik-zp commented Jul 25, 2017

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

@cuanjooste
Copy link

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
Copy link
Owner

nik-zp commented Jul 28, 2017

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

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

@cuanjooste
Copy link

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

@nik-zp nik-zp closed this as completed Aug 1, 2017
@pbpraha
Copy link
Author

pbpraha commented Aug 1, 2017

Thanks also ! :)

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

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