eclipse-paho / paho.mqtt.golang

Other
2.77k stars 534 forks source link

Need a function to get the ClientID of the message sender #674

Closed igkg closed 5 months ago

igkg commented 5 months ago

Hello, We have emqx plugin and when we receive a message we save ClientID of message sender and use it next in our code. But as I see current library doesn't provide ClientID of message sender.

type Message interface {
    Duplicate() bool
    Qos() byte
    Retained() bool
    Topic() string
    MessageID() uint16
    Payload() []byte
    Ack()
}

Could someone add a feature to get the ClientId of the sender of a message?

MattBrittan commented 5 months ago

This should be relatively simple to implement yourself outside of the library (the callback is passed a MessageHandler which accepts (Client, Message) so you can get the client ID using Client.OptionsReader().ClientID(). I don't see a need to change the Message interface` here.

igkg commented 5 months ago

@MattBrittan

var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
    clientReader := client.OptionsReader()
    clientId := clientReader.ClientID()

clientId is the recipient client ID. I need the sender client ID.

MattBrittan commented 5 months ago

Then you will need to include it in the message or topic. The Client ID is only used for comms between one client and the broker, it's not passed to subscribing clients (so there is no way for the receiving client to get this info). This is setout in the MQTT spec.

igkg commented 5 months ago

Okay, got it. Thanks for your reply.