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

On event #13

Open
Sunny0218 opened this issue Jun 12, 2023 · 3 comments
Open

On event #13

Sunny0218 opened this issue Jun 12, 2023 · 3 comments

Comments

@Sunny0218
Copy link

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.

@tommy44458
Copy link
Owner

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

@Sunny0218
Copy link
Author

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

@tommy44458
Copy link
Owner

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

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

2 participants