mqtt-vue-hook icon indicating copy to clipboard operation
mqtt-vue-hook copied to clipboard

On event

Open Sunny0218 opened this issue 2 years ago • 3 comments

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.

Sunny0218 avatar Jun 12 '23 03:06 Sunny0218

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',
)

tommy44458 avatar Jun 13 '23 04:06 tommy44458

Thanks, I was going to try it out. I've always had a question, what does the third parameter "string_key" actually do?

Sunny0218 avatar Jun 13 '23 08:06 Sunny0218

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',
)

tommy44458 avatar Jun 13 '23 09:06 tommy44458