Azure / go-amqp

AMQP 1.0 client library for Go.
https://github.com/Azure/go-amqp
MIT License
104 stars 56 forks source link

Support sending message with text format only #286

Closed polaraditia closed 1 year ago

polaraditia commented 1 year ago

Please implement Message struct to support text message format payload (not bytes) since other MQ library for other language like NMS for C# have both ITextMessage and IByteMessage interface.

Actually, in current code, we can still stored the text payload in message.Value later after we create message using NewMessage([]byte) which stored payload in message.Data. But some middlewares like I tested using ActiveMQ Artemis, it can't access (or maybe just ignored (?)) text stored in message.Value and only display message bytes stored in message.Data .

MicrosoftTeams-image

MicrosoftTeams-image (1)

As you can see in screenshot above, the message type (in Headers.type property) is detected as bytes and only displaying body with data stored in message.Data (contains Hello byte) not in message.Value (contains Hello text).

I tested creating consumer using C# with NMS library that try to consume the message treated as TextMessage with hope it can access text format parts of the message and the result is it can't read the message. And if the message is treated as ByteMessage it can access Hello byte only, not Hello text.

As workaround, we can make new additional NewMessage function with takes text parameter instead of []byte

Current code of message.go, message payload stored in bytes in Data field:

func NewMessage(data []byte) *Message {
    return &Message{
        Data: [][]byte{data},
    }
}

We need add additional NewMessageText function with text parameter, message payload stored in string in Value field:

func NewMessageText(data string) *Message {
    return &Message{
        Value: data,
    }
}

So it leave the Data field in message object blank/uninitialized (?) and only data Value contains payload. I tested using ActiveMQ Artemis, and finally, it can display message text stored in message.Value (contains Hello text) with detected message type as text not byte.

MicrosoftTeams-image (2)

Thank you

jhendrixMSFT commented 1 year ago

Per section 3.2 of the AMQP 1.0 spec, the data section, amqp-sequence section, or value section are mutually exclusive. To send a message using the value section, simply create a new Message and populate the Value field, no constructor required (will update the docs to clarify this).

msg := &Message{
    Value: "something",
}
jhendrixMSFT commented 1 year ago

Please ping back if you have further questions.

polaraditia commented 1 year ago

Thanks for your explanations and updated docs