mqtt-vue-hook
mqtt-vue-hook copied to clipboard
On event
Hello, it's me again.How can I on different events like native mqtt? For example:client.on('connect', function () {}).I want to be able to do the corresponding business logic as soon as I connect.
Hi @Sunny0218,
Thanks for your questions. You can try the following code:
mqttHook.registerEvent(
'on-connect', // mqtt status: on-connect, on-reconnect, on-disconnect, on-connect-fail
(topic: string, message: string) => {
console.log('mqtt connected')
},
'string_key',
)
Thanks, I was going to try it out. I've always had a question, what does the third parameter "string_key" actually do?
When registering the same topic event in multiple components, you can differentiate them using the "string_key". For example:
// login.ts
mqttHook.registerEvent(
'on-connect',
(topic: string, message: string) => {
// do something
},
'login',
)
// dashboard.ts
mqttHook.registerEvent(
'on-connect',
(topic: string, message: string) => {
// do something
},
'dashboard',
)