mochi-mqtt / server

The fully compliant, embeddable high-performance Go MQTT v5 server for IoT, smarthome, and pubsub
MIT License
1.28k stars 220 forks source link

Proposal: Add an Example for Custom Hook Usage #407

Closed TheRiseOfDavid closed 1 month ago

TheRiseOfDavid commented 5 months ago

Hi,

I really like this project and I would love to contribute to it. I noticed there isn't an example for using custom hooks, which made it challenging for me to figure out the correct way to implement them. I believe other newcomers might face similar difficulties.

To address this, I would like to add an example demonstrating how to use custom hooks.

Could you please guide me on how to submit to update the README.md by pull request accordingly?

Thank you!

Maybe the example like hook.go

type authHook struct {
    mqtt.HookBase
}

func (h *authHook) OnConnectAuthenticate(cl *mqtt.Client, pk packets.Packet) bool {
    if cl.Properties.Username == 'david' {
              return true
        }
        return true
}

server.go

//something to build the mqtt 
_ = server.AddHook(new(authHook), nil)
//something to start the mqtt 
err := server.Serve()
dgduncan commented 4 months ago

@TheRiseOfDavid Thank you for your interest in the project! What do you feel is missing in examples such as looking here that could be provided in this new update of the readme?

TheRiseOfDavid commented 4 months ago

@dgduncan I feel embarrassed to open this issue. When I was a noob using this package, I didn't realize that auth.Hook is an encapsulation of HookBase. Because auth.Hook is not in the /example/auth folder but rather in hook/auth, I was confused and thought this project didn't have a hook example. Thank you.